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

问题描述:

我已经搜索了这个问题,但我只遇到了似乎难以根据我的特定需求量身定制的非常具体的答案.

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

假设我试图重写的 URL 是这样的:

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

我该怎么做?

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

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]

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

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]