Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

.htaccess - strip query string issue : redirect html website to mobile by htaccess

in a previous post on stackoverflow there is this code, but it wasn't comprehensive. Something was missing. I copy this code into htaccess, and it is working fine now because I add also the mod_rewrite module. Works splendid now.

Copy this code into your .htaccess and just change the name of domain. And you must wait couple of hours for DNS changes to propagate the Internet.

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera  mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ https://m.neculaifantanaru.com%{REQUEST_URI}? [R,L]

############ https://neculaifantanaru.com ############

AddHandler application/x-httpd-ea-php56 .html .htm    
AddType application/x-httpd-ea-php56 .html .htm
 
<IfModule mod_rewrite.c>
RewriteEngine On
<IfModule mod_autoindex.c>
IndexIgnore *
</ifModule>
</ifModule>
question from:https://stackoverflow.com/questions/65944432/strip-query-string-issue-redirect-html-website-to-mobile-by-htaccess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...