问题
I have a website with an old type of Single Sign On. A parent website sends users to my website with a URL like:
http://test.instela.fm/index.php?gid=abcd1234&u=thedewil&id=11472
and I want to rewrite it as this:
http://test.instela.fm/login/abcd1234/thedewil/11472
I have created a .htaccess file as following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php?gid=(.*)&u=(.*)&id=(.*)$ login/$1/$2/$3 [L]
I tried escaping the question mark in the URL as this:
RewriteRule ^index.php\?gid=(.*)&u=(.*)&id=(.*)$ login/$1/$2/$3 [L]
But, unfortunately both these rules don't match and rewrite the URL, which I can't understand why.
回答1:
You can not match against url QueryString in pattern of a RewriteRule directive. You can only match against url path ( ie : index.php
) in RewriteRule. Url part after the ?
sign is URL QueryString. you will need to match against %{QUERY_STRING}
variable in RewriteCond
.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^gid=([^&]+)&u=([^&]+)&id=([^&]+)$ [NC]
RewriteRule ^index.php$ /login/%1/%2/%3? [L,R]
来源:https://stackoverflow.com/questions/50341099/htaccess-doesnt-escape-question-mark-on-rewriterule