Skip to content

Instantly share code, notes, and snippets.

@arellano-gustavo
Last active October 18, 2022 14:09
Show Gist options
  • Save arellano-gustavo/7c1e284e5f22d8ca7ffe0d8d5cf1e850 to your computer and use it in GitHub Desktop.
Save arellano-gustavo/7c1e284e5f22d8ca7ffe0d8d5cf1e850 to your computer and use it in GitHub Desktop.
Nginx configuration for redirect to a mobile version if mobile detected
#-----------------------------
# alvinalexander.com (desktop)
#-----------------------------
server {
server_name alvinalexander.com;
listen 80;
#----- redirect to mobile check (starts) -----#
set $mobile_rewrite do_not_perform;
# this regex string is actually much longer to match more mobile devices
if ($http_user_agent ~* "|android|ip(ad|hone|od)|kindle") {
set $mobile_rewrite perform;
}
if ($mobile_rewrite = perform) {
rewrite ^ http://m.alvinalexander.com$request_uri? redirect;
break;
}
#----- redirect to mobile check (ends) -----#
include /etc/nginx/aa-common.conf;
}
#------------------------------
# m.alvinalexander.com (mobile)
#------------------------------
server {
server_name m.alvinalexander.com;
listen 80;
include /etc/nginx/aa-common.conf;
}
# FROM: https://alvinalexander.com/technology/nginx-redirect-mobile-devices-users-mobile-website/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment