自定义.htaccess移动重定向

大兄弟6

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

htaccess 301自定义重定向

来自分类Dev

通过htaccess重定向自定义URL

来自分类Dev

在自定义AsyncControllerActionInvoker中重定向

来自分类Dev

从自定义模板重定向WordPress

来自分类Dev

nginx默认重定向和自定义重定向

来自分类Dev

nginx默认重定向和自定义重定向

来自分类Dev

将根/主页重定向到自定义页面会破坏.htaccess中的所有其他重定向

来自分类Dev

htaccess中的自定义正则表达式重定向

来自分类Dev

如何使用.htaccess中的自定义逻辑重定向url?

来自分类Dev

htaccess重定向https和http和自定义网址

来自分类Dev

.htaccess不会重定向到自定义404页面

来自分类Dev

自定义重定向仅适用于通过htaccess的特殊格式

来自分类Dev

如何使用.htaccess中的自定义逻辑重定向url?

来自分类Dev

htaccess中的自定义正则表达式重定向

来自分类Dev

多语言自定义404 htaccess规则导致重定向循环

来自分类Dev

.htaccess不会重定向到自定义404页面

来自分类Dev

自定义重定向仅适用于通过htaccess的特殊格式

来自分类Dev

htaccess无法重定向到自定义404页面

来自分类Dev

使用正则表达式自定义 htaccess 301 重定向

来自分类Dev

apache/.htaccess:不正确的页面不会重定向到自定义 404

来自分类Dev

htaccess 重定向到自定义地址,而不是显示禁止错误消息

来自分类Dev

自定义移动导航

来自分类Dev

.htaccess重定向代码将重定向到未找到的自定义404,而不是将www重定向到非www版本

来自分类Dev

从另一个自定义重定向到自定义 html 页面

来自分类Dev

从自定义Adminhtml控制器重定向

来自分类Dev

成功登录后Shibboleth自定义重定向

来自分类Dev

Grails安全插件自定义重定向

来自分类Dev

如何停止重定向到自定义域?

来自分类Dev

如何使用Nginx重定向到自定义网址?

Related 相关文章

  1. 1

    htaccess 301自定义重定向

  2. 2

    通过htaccess重定向自定义URL

  3. 3

    在自定义AsyncControllerActionInvoker中重定向

  4. 4

    从自定义模板重定向WordPress

  5. 5

    nginx默认重定向和自定义重定向

  6. 6

    nginx默认重定向和自定义重定向

  7. 7

    将根/主页重定向到自定义页面会破坏.htaccess中的所有其他重定向

  8. 8

    htaccess中的自定义正则表达式重定向

  9. 9

    如何使用.htaccess中的自定义逻辑重定向url?

  10. 10

    htaccess重定向https和http和自定义网址

  11. 11

    .htaccess不会重定向到自定义404页面

  12. 12

    自定义重定向仅适用于通过htaccess的特殊格式

  13. 13

    如何使用.htaccess中的自定义逻辑重定向url?

  14. 14

    htaccess中的自定义正则表达式重定向

  15. 15

    多语言自定义404 htaccess规则导致重定向循环

  16. 16

    .htaccess不会重定向到自定义404页面

  17. 17

    自定义重定向仅适用于通过htaccess的特殊格式

  18. 18

    htaccess无法重定向到自定义404页面

  19. 19

    使用正则表达式自定义 htaccess 301 重定向

  20. 20

    apache/.htaccess:不正确的页面不会重定向到自定义 404

  21. 21

    htaccess 重定向到自定义地址,而不是显示禁止错误消息

  22. 22

    自定义移动导航

  23. 23

    .htaccess重定向代码将重定向到未找到的自定义404,而不是将www重定向到非www版本

  24. 24

    从另一个自定义重定向到自定义 html 页面

  25. 25

    从自定义Adminhtml控制器重定向

  26. 26

    成功登录后Shibboleth自定义重定向

  27. 27

    Grails安全插件自定义重定向

  28. 28

    如何停止重定向到自定义域?

  29. 29

    如何使用Nginx重定向到自定义网址?

热门标签

归档