Redirect to a directory, preserving the query string

扶醉桌前 提交于 2019-12-12 01:47:01

问题


Any request for / that has a query string is causing a 404 to be triggered. I've found that if I add a /shop/ before the query string, it gets redirected internally and no 404 is triggered.

I need a general mod_rewrite rule that will take a URL in the form of:

http://www.example.com/?foo=bar

and redirect it to:

http://www.example.com/shop/?foo=bar

where the query string (could be anything) is preserved.


回答1:


This will redirect (URL will change) all hits into ROOT (e.g. http://www.example.com/) that has query string into http://www.example.com/shop/.

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^$ http://www.example.com/shop/ [QSA,R,L]

This will rewrite internally (URL will stay the same in browser) all hits into ROOT (e.g. http://www.example.com/) that has query string into http://www.example.com/shop/.

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^$ /shop/ [QSA,L]


来源:https://stackoverflow.com/questions/6527156/redirect-to-a-directory-preserving-the-query-string

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