.htaccess doesn't escape question mark on RewriteRule

社会主义新天地 提交于 2021-02-07 20:54:17

问题


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

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