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

时间:2023-02-09 10:51:54

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:

假设我要重写的URL是:

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 个解决方案

#1


19  

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:

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

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:

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

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

#1


19  

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:

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

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:

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

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