【问题标题】:Custom .htaccess mobile redirection自定义 .htaccess 移动重定向
【发布时间】:2014-10-15 10:47:40
【问题描述】:

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

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

【问题讨论】:

    标签: apache .htaccess mobile rewrite


    【解决方案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 ===
    

    基本上,我们已经否定了这些条件,如果其中任何一个通过,我们就知道它不是移动的。

    【讨论】:

    • 好的,谢谢您的帮助。我已经删除了前导斜杠(也编辑了你的答案以删除它们),现在它说页面有一个重定向循环并重定向到“/index/mobile”我猜这是发生的,因为它是最后一次RewriteRule 即使它在遇到第一个时应该停止。作为参考,我正在尝试这个网址:/index/tracks
    • @BigBrother6 重写引擎将继续循环遍历所有规则,直到 URI 停止更改(或达到内部重定向限制,导致 500)。因为(.*) 也匹配/index/mobile。你正在创建一个无限循环。相反,请尝试:^(?!index/mobile/)(.*)
    • 成功了!你是绝对的救星。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 2013-04-22
    • 1970-01-01
    • 2011-11-08
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多