自定义.htaccess移动重定向 [英] Custom .htaccess mobile redirection

查看:77
本文介绍了自定义.htaccess移动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅在用户通过移动设备访问网站时才尝试将特定页面重定向到其他特定页面.到目前为止,我有下面的代码,但是它不起作用,我不确定原因.请告知.

I am trying to redirect specific pages to other specific pages only when the user is accessing the website via a mobile device. So far, I have the code below, but it is not working and I am unsure as to why. Please advise.

#=== 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 ===

最终解决方案在这里发布.感谢乔恩·林

Final solution posted here. Thanks to Jon Lin

#=== 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 ===

推荐答案

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

Rewrite conditions only apply to the immediately following rewrite rule, which means that all those conditions only apply to this rule:

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

,并且您在htaccess文件中拥有的所有其他规则没有附加条件.因此,无论是否有移动请求,所有请求都将适用这些规则.

and all those other rules you have in your htaccess file has no conditions attached to them. So all requests, mobile or not, will have those rules apply to them.

您可以对条件求反并进行传递,而不必为所有规则复制该大块代码.此外,您还想从规则的正则表达式中删除斜杠.

Instead of duplicating that huge block for all of your rules, you can negate the condition and do a passthrough. Additionally, you want to remove the leading slash from your rule's regexes.

#=== 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 ===

从本质上讲,我们已经消除了条件,如果其中任何一个通过,我们就会知道它不是可移动的.

Essentially, we've negated the conditions, and if any of them pass, we know it's not mobile.

这篇关于自定义.htaccess移动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆