自定义的.htaccess移动重定向(Custom .htaccess mobile redirec

2019-10-21 03:10发布

我想只有当用户通过移动设备访问该网站重定向特定页面其他特定网页。 到目前为止,我有下面的代码,但它不工作,我不确定为什么。 请指教。

#=== Start Mobile Redirection ===
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)noredirect=true(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*noredirect=true.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^/index/albums/review(.*)$ /index/mobile/album$1 [R,L]
RewriteRule ^/index/albums/upcoming(.*)$ /index/mobile/albums-upcoming$1 [R,L]
RewriteRule ^/index/albums(.*)$ /index/mobile/albums$1 [R,L]
RewriteRule ^/index/artists/info(.*)$ /index/mobile/artist$1 [R,L]
RewriteRule ^/index/hip-hop/best(.*)$ /index/mobile/best$1 [R,L]
RewriteRule ^/index/music-charts/albums(.*)$ /index/mobile/chart-albums$1 [R,L]
RewriteRule ^/index/music-charts/entry(.*)$ /index/mobile/chart$1 [R,L]
RewriteRule ^/index/music-charts(.*)$ /index/mobile/charts$1 [R,L]
RewriteRule ^/index/hip-hop/downloads(.*)$ /index/mobile/downloads$1 [R,L]
RewriteRule ^/index/news/entry(.*)$ /index/mobile/feature$1 [R,L]
RewriteRule ^/index/news(.*)$ /index/mobile/features$1 [R,L]
RewriteRule ^/index/mixtapes/entry(.*)$ /index/mobile/mixtape$1 [R,L]
RewriteRule ^/index/mixtapes(.*)$ /index/mobile/mixtapes$1 [R,L]
RewriteRule ^/index/playlists/entry(.*)$ /index/mobile/playlist$1 [R,L]
RewriteRule ^/index/playlists(.*)$ /index/mobile/playlists$1 [R,L]
RewriteRule ^/index/tracks/review(.*)$ /index/mobile/track$1 [R,L]
RewriteRule ^/index/tracks(.*)$ /index/mobile/tracks$1 [R,L]
RewriteRule ^/(.*) /index/mobile/ [R,L]

#=== End mobile Redirection ===

编辑:最终的解决方案张贴在这里。 由于乔恩林

#=== Start Mobile Redirection ===
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$) [OR]

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}  ^.*noredirect=true.*$ [NC]
RewriteRule ^ - [L]

RewriteCond %{HTTP:x-wap-profile} ^$ 
RewriteCond %{HTTP:Profile}       ^$ 
RewriteCond %{HTTP_USER_AGENT} !"acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC]
RewriteCond %{HTTP_USER_AGENT} !"palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC]
RewriteCond %{HTTP_USER_AGENT} !"sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC]
RewriteCond %{HTTP_USER_AGENT} !"teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC]
RewriteCond %{HTTP_USER_AGENT} !"wapp|wapr|webc|winw|winw|xda|xda-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"up.browser|up.link|windowssce|iemobile|mini|mmp" [NC]
RewriteCond %{HTTP_USER_AGENT} !"symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^ - [L]

# Now redirect to the mobile site
RewriteRule ^index/albums/review(.*)$ /index/mobile/album$1 [R,L]
RewriteRule ^index/albums/upcoming(.*)$ /index/mobile/albums-upcoming$1 [R,L]
RewriteRule ^index/albums(.*)$ /index/mobile/albums$1 [R,L]
RewriteRule ^index/artists/info(.*)$ /index/mobile/artist$1 [R,L]
RewriteRule ^index/hip-hop/best(.*)$ /index/mobile/best$1 [R,L]
RewriteRule ^index/music-charts/albums(.*)$ /index/mobile/chart-albums$1 [R,L]
RewriteRule ^index/music-charts/entry(.*)$ /index/mobile/chart$1 [R,L]
RewriteRule ^index/music-charts(.*)$ /index/mobile/charts$1 [R,L]
RewriteRule ^index/hip-hop/downloads(.*)$ /index/mobile/downloads$1 [R,L]
RewriteRule ^index/news/entry(.*)$ /index/mobile/feature$1 [R,L]
RewriteRule ^index/news(.*)$ /index/mobile/features$1 [R,L]
RewriteRule ^index/mixtapes/entry(.*)$ /index/mobile/mixtape$1 [R,L]
RewriteRule ^index/mixtapes(.*)$ /index/mobile/mixtapes$1 [R,L]
RewriteRule ^index/playlists/entry(.*)$ /index/mobile/playlist$1 [R,L]
RewriteRule ^index/playlists(.*)$ /index/mobile/playlists$1 [R,L]
RewriteRule ^index/tracks/review(.*)$ /index/mobile/track$1 [R,L]
RewriteRule ^index/tracks(.*)$ /index/mobile/tracks$1 [R,L]
RewriteRule ^(?!index/mobile/)(.*) /index/mobile/ [R,L]

#=== End mobile Redirection ===

Answer 1:

重写条件仅适用于紧随其后的重写规则,这意味着所有这些条件适用于本规则:

RewriteRule ^/index/albums/review(.*)$ /index/mobile/album$1 [R,L]

和所有你在你的htaccess文件有那些规则没有附带条件。 因此,所有的请求,移动与否,都会有这些规则适用于他们。

而不是重复的所有规则是块巨大的,就可以否定条件,做一个直通。 此外,要删除从规则的正则表达式的斜线。

#=== Start Mobile Redirection ===
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

RewriteCond %{HTTP_USER_AGENT} macintosh [NC,OR]

# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$) [OR]

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}  ^.*noredirect=true.*$ [NC]
RewriteRule ^ - [L]

RewriteCond %{HTTP:x-wap-profile} ^$ 
RewriteCond %{HTTP:Profile}       ^$ 
RewriteCond %{HTTP_USER_AGENT} !"acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC]
RewriteCond %{HTTP_USER_AGENT} !"palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC]
RewriteCond %{HTTP_USER_AGENT} !"sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC]
RewriteCond %{HTTP_USER_AGENT} !"teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC]
RewriteCond %{HTTP_USER_AGENT} !"wapp|wapr|webc|winw|winw|xda|xda-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"up.browser|up.link|windowssce|iemobile|mini|mmp" [NC]
RewriteCond %{HTTP_USER_AGENT} !"symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteRule ^ - [L]

# Now redirect to the mobile site
RewriteRule ^index/albums/review(.*)$ /index/mobile/album$1 [R,L]
RewriteRule ^index/albums/upcoming(.*)$ /index/mobile/albums-upcoming$1 [R,L]
RewriteRule ^index/albums(.*)$ /index/mobile/albums$1 [R,L]
RewriteRule ^index/artists/info(.*)$ /index/mobile/artist$1 [R,L]
RewriteRule ^index/hip-hop/best(.*)$ /index/mobile/best$1 [R,L]
RewriteRule ^index/music-charts/albums(.*)$ /index/mobile/chart-albums$1 [R,L]
RewriteRule ^index/music-charts/entry(.*)$ /index/mobile/chart$1 [R,L]
RewriteRule ^index/music-charts(.*)$ /index/mobile/charts$1 [R,L]
RewriteRule ^index/hip-hop/downloads(.*)$ /index/mobile/downloads$1 [R,L]
RewriteRule ^index/news/entry(.*)$ /index/mobile/feature$1 [R,L]
RewriteRule ^index/news(.*)$ /index/mobile/features$1 [R,L]
RewriteRule ^index/mixtapes/entry(.*)$ /index/mobile/mixtape$1 [R,L]
RewriteRule ^index/mixtapes(.*)$ /index/mobile/mixtapes$1 [R,L]
RewriteRule ^index/playlists/entry(.*)$ /index/mobile/playlist$1 [R,L]
RewriteRule ^index/playlists(.*)$ /index/mobile/playlists$1 [R,L]
RewriteRule ^index/tracks/review(.*)$ /index/mobile/track$1 [R,L]
RewriteRule ^index/tracks(.*)$ /index/mobile/tracks$1 [R,L]
RewriteRule ^(.*) /index/mobile/ [R,L]

#=== End mobile Redirection ===

从本质上讲,我们已经否定了条件,如果其中任何一个传递,我们知道这是不是移动。



文章来源: Custom .htaccess mobile redirection