.htaccess将查询字符串重写为路径

时间:2012-10-21 23:41:52

标签: apache .htaccess mod-rewrite

我已经搜索过这个问题,但我只是遇到了很难根据我的具体需求量身定制的具体答案。

假设我正在尝试重写的URL是:

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

我想重写它,使其显示为:

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

我该怎么做?

1 个答案:

答案 0 :(得分:22)

为了将/test/something之类的请求路由到内部重写以便/test.php?whatever=something中的内容得到服务,您可以在文档根目录中的htaccess文件中使用这些规则:

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

为了将查询字符串URL重定向到更好看的URL:

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