.htaccess rewrite query string as path

我的梦境 提交于 2019-12-27 11:31:09

问题


I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.

Let's say the URL I'm attempting to rewrite is this:

http://www.example.org/test.php?whatever=something

I want to rewrite it so that it appears as this:

http://www.example.org/test/something

How can I do this?


回答1:


In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]

And in order to redirect the query string URL to the nicer looking one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]


来源:https://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path

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