Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 自定义.htaccess移动重定向_Apache_.htaccess_Mobile_Rewrite - Fatal编程技术网

Apache 自定义.htaccess移动重定向

Apache 自定义.htaccess移动重定向,apache,.htaccess,mobile,rewrite,Apache,.htaccess,Mobile,Rewrite,只有当用户通过移动设备访问网站时,我才尝试将特定页面重定向到其他特定页面。到目前为止,我有下面的代码,但它不工作,我不确定为什么。请告知 #=== Start Mobile Redirection === # Check if this is the noredirect query string RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$) # Set a cookie, and skip the next rule

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

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

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

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”,我猜这是因为它正在命中最后一个重写规则,即使它在命中第一个规则时应该停止。作为参考,我正在尝试此url:/index/tracks@BigBrother6重写引擎将继续循环遍历所有规则,直到URI停止更改(或者达到内部重定向限制,这将导致500个错误)。因为
(.*)
也匹配
/index/mobile
。您正在创建一个无限循环。相反,请尝试:
^(?!index/mobile/)(.*)
,这很有效!你绝对是个救命恩人。非常感谢你。