Apache .htaccess: How to remove question mark from

2019-02-15 13:12发布

How to make .htaccess to remove question mark from URL if not ?id=(.*)?

# Rewrite for ?id=(.*)
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]

# It does not work out on this way
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !^id=.*
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]

3条回答
女痞
2楼-- · 2019-02-15 13:43

Does this work?

RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^(.*)$ $1?%1 [R=301,L]

Tip: during your testings, use 302 redirections instead of 301, as 301 redirections are stored by browsers. You can finally switch to classic 301 when you are done testing.

查看更多
看我几分像从前
3楼-- · 2019-02-15 13:46

This would be the right rule:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond !{QUERY_STRING} id
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]

Update:

# Query rewrite exceptions
RewriteCond %{QUERY_STRING} !callback=.*
查看更多
We Are One
4楼-- · 2019-02-15 13:47

If you need http://site.com/page/?YOURSTRING=blabla

redirected to http://site.com/page/

then see this link - https://stackoverflow.com/a/15680832/2215124

查看更多
登录 后发表回答