简体   繁体   中英

Remove ?query=string from URL with htaccess

I used to have a query string for handling mobile pages. Now the site is responsive it doesn't need it and I need to do 301 redirects let google know.

so

http://www.example.com/aaa/bbb?fromMobile=true should become http://www.example.com/aaa/bbb

and

http://www.example.com/aaa?fromMobile=true should become http://www.example.com/bbb

and

http://www.example.com?fromMobile=true should become http://www.example.com

etc

I have:

RewriteCond %{QUERY_STRING} ^(?)fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]

But this redirects:

http://www.example.com?fromMobile=true to http://www.example.com ?

How can I get rid of the trailing question mark? I've tried a bunch of things without joy.

You can use:

RewriteCond %{QUERY_STRING} ^fromMobile=true$ [NC]
RewriteRule (.*) /$1? [R=301,L]

This is assuming there is no other query parameter besides fromMobile=true .

If there is a chance of other query parameters also then use:

RewriteCond %{QUERY_STRING} ^(.+?&)?fromMobile=true(?:&(.*))?$ [NC]
RewriteRule (.*) /$1?%1%2 [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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