简体   繁体   English

.htaccess 移动重定向在第一次移动点击后被所有访问者看到

[英].htaccess mobile redirect seen by all visitors after first mobile hit

I very recently migrated to a new web host.我最近迁移到了新的 web 主机。

I have a redirect in htaccess, that sends mobile users to a mobile site, based on the detected user_agent.我在 htaccess 中有一个重定向,它根据检测到的 user_agent 将移动用户发送到移动站点。 This htaccess and associated website/s worked perfectly with my old host.这个 htaccess 和相关网站与我的旧主机完美配合。

With my current host, whenever I enable this rewrite rule, as soon as a mobile user visits the root of desktop site, they as expected, get redirected to the mobile site however, all subsequent requests to the desktop site on any device, get redirected to the mobile site.对于我当前的主机,每当我启用此重写规则时,只要移动用户访问桌面站点的根目录,他们就会按预期重定向到移动站点,但是,任何设备上对桌面站点的所有后续请求都会被重定向到移动网站。 ie it appears that the redirect to the mobile is cached and that cached redirect then gets served to all visitors regardless of user_agent until I disable the htaccess and they clear their caches.即,似乎对移动设备的重定向被缓存,然后缓存的重定向被提供给所有访问者,无论 user_agent 是什么,直到我禁用 htaccess 并且他们清除缓存。

My hosting company say that they are not developers and they cannot help with my code, that there is something wrong with it that's causing this issue but they can't tell me what.我的托管公司说他们不是开发人员,他们无法帮助我的代码,它有什么问题导致了这个问题,但他们不能告诉我是什么。 :-| :-|

Please could someone cast their eyes over the below and tell me what if anything, is wrong with it.请有人将目光投向下面,并告诉我有什么问题。

ps.附言。 This is shared hosting so i don't have access to the logs.这是共享主机,所以我无权访问日志。

RewriteEngine on
# http://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]


# https://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]


## Redirect to mobile site

RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|mobile.+firefox|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ ce|xda|xiino [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-) [NC]

RewriteCond %{HTTP_HOST} !^mobile\. [NC]
RewriteRule ^(.*)$ https://mobile.mywebsite.co.uk [E=NO_CACHE,R,L]

Header always set Cache-Control "private, no-cache" env=NO_CACHE

If you are making redirect decisions based on the user agent, you need to tell caching proxy servers and CDNs that they need to check to see how each user agent redirects rather than returning the same result as the first user.如果您基于用户代理做出重定向决策,您需要告诉缓存代理服务器和 CDN,他们需要检查每个用户代理如何重定向,而不是返回与第一个用户相同的结果。

To do that in .htaccess you can just add the line:要在 .htaccess 中做到这一点,您只需添加以下行:

Header append Vary User-Agent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM