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

2023-09-02 00:19:40 作者:我苟延残喘的活着。

我搜索了这个问题,但我只碰到过,似乎很难裁缝给我的特殊需求真正具体的答案。

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

我怎样才能做到这一点?

How can I do this?

推荐答案

为了路由就像一个请求 /测试/某事在内部重新编写,以便在内容 /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重定向到更好看之一:

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]