Skip to content

Instantly share code, notes, and snippets.

@sheeplogh
Created August 30, 2012 01:42
Show Gist options
  • Save sheeplogh/3521534 to your computer and use it in GitHub Desktop.
Save sheeplogh/3521534 to your computer and use it in GitHub Desktop.
[mod_rewrite]Redirect HTTP requests by UA(PC/Smartphone/...)
## SET ENV "UA" ##
### DEFAULT
RewriteRule .* - [E=UA:default]
### PC
RewriteCond %{HTTP_USER_AGENT} Windows [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Mac\sOS\sX [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Macintosh [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Mac_ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Linux [NC,OR]
RewriteCond %{HTTP_USER_AGENT} BlackBerry [NC]
RewriteRule .* - [E=UA:pc]
### SmartPhone
RewriteCond %{HTTP_USER_AGENT} Windows\sPhone [NC,OR]
RewriteCond %{HTTP_USER_AGENT} like\sMac\sOS\sX [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Android [NC]
RewriteRule .* - [E=UA:sp]
### CDN edge server
RewriteCond %{HTTP_USER_AGENT} Amazon\sCloudFront [NC]
RewriteRule .* - [E=UA:cdn]
## REDIRECTION ##
### PC
RewriteCond %{ENV:UA} pc
RewriteRule ^/(.*)$ /pc/$1 [R,L]
### SmartPhone
RewriteCond %{ENV:UA} sp
RewriteRule ^/(.*)$ /sp/$1 [R,L]
### CDN edge server
RewriteCond %{ENV:UA} cdn
RewriteRule .* - [L]
### Others
RewriteCond %{ENV:UA} default
RewriteRule ^/(.*)$ /pc/$1 [R,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment