Mobile Redirect to desktop version using htaccess

送分小仙女□ 提交于 2020-01-11 14:06:23

问题


I have a website called

www.example.com

I have a mobile website called

m.example.com

firstly i want to redirect automatically from desktop version to mobile site (if it is mobile only.. i want to detect all mobile versions)

then I want to use an htaccess to automatically redirect the main website URL to the mobile version.. However, there is a link on the mobile version that points back to the main website called

www.example.com/?nm=1 (nm mean nomobile)

i want to set cookie for this one.. (redirect to desktop site from mobile site)

if user come again after passing time ago.. i want to check hv cookie.. (check www.example.com/?nm=1 set cookie earlier) if havent cookie automatically redirect to mobile version... if have cookie want to stay in desktop version.

How can I accomplish this via htaccess without JavaSCript.


回答1:


This answer solves part of the problem for you. Just like that answer, I am not going to suggest a list with user-agents for you. Find a list that is suitable for you, and put it in the rule. The types of mobile devices, browsers in mobile devices etc is ever-changing, and this answer would be out-dated before I even posted it. You need to adapt that rule a little to prevent it from matching if the cookie "nomobile" is set to "1". You need a rule that sees the "nm=1" in the url and sets the cookie. You probably also want some kind of reset for that oookie, which I labeled "nm=0".

RewriteCond %{QUERY_STRING} nm=1
RewriteRule ^ - [CO=nomobile:1:localhost:10000]

RewriteCond %{QUERY_STRING} nm=0
RewriteRule ^ - [CO=nomobile:0:localhost:10000]

RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteCond %{HTTP_COOKIE} !nomobile=1
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]

Please check the documentation for cookies for the correct usage of the CO-flag on your specific site. The code above is not tested, but in theory it should work.



来源:https://stackoverflow.com/questions/23789858/mobile-redirect-to-desktop-version-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!