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

2023-09-02 00:36:35 作者:幼稚泡泡猪

我怎么会用一个重写改变:

How could I use a rewrite to change:

/?标记=富

要:

/标签/富

我想:

RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]

但没有奏效。

But it did not work.

推荐答案

要避免递归,你应该检查的请求线,而不是的如%{QUERY_STRING} 可能已经被另一个规则已经改变的查询字符串:

To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:

RewriteCond %{THE_REQUEST} ^GET /?(([^&s]*&)*)tag=([^&s]+)&?([^s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]

然后你可以重写请求回内部没有矛盾:

Then you can rewrite that requests back internally without conflicts:

RewriteRule ^tag/(.*) index.php?tag=$1 [L]