用于搜索引擎友好网址的递归 mod_rewrite

2021-12-21 00:00:00 递归 php .htaccess apache mod-rewrite

我一直在阅读以前的解决方案 递归 mod_rewrite 问题类似于我正在尝试做的,不同之处在于我通过 index.php 文件发送所有查询,因此不需要在询问.

I've been reading through a previous solution to a recursive mod_rewrite problem that is similar to what I'm trying to do, the difference is I'm sending all queries through an index.php file and so don't need to specify the script within the query.

本质上我想在搜索引擎友好的 url 中递归转换任意数量的参数:

Essentially I want to recursively convert any number of parameters within a search engine friendly url:

example.com/param1/val1/param2/val2/...

到常规查询字符串:

example.com/index.php?param1=val1&param2=val2&...

到目前为止,我的尝试都没有成功:

So far I've been unsuccessful in in my attempts though:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)/([^/]+) $1=$2&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?%1 [L]

有人可以提供任何建议吗?

Would anyone be able to offer any suggestions?

推荐答案

我从另一个问题复制了解决方案并像这样修改:

I copied the solution from that other question and modified it like this:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)?([^/]+)/([^/]+) $1?$2=$3&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^.*$ index.php?%1 [L]

它几乎做同样的事情,除了在第一个规则中,第一个匹配是可选的,在第二个规则中,匹配在所有其他对匹配之后剩下的任何部分.

It does nearly the same thing, except in the first rule, the first match is optional and in the second rule, the match is on whatever is left after all the other pairs are matched.

对于奇数个参数,第一个参数被忽略.

For an odd number of parameters, the first parameter is ignored.

注意,如果您希望有很多参数,您可能需要更改一些设置.

One note, if you expect to have a lot of parameters, you may have to change some settings.

将类似的内容添加到您的 .htaccess 文件中

Add something like this to your .htaccess file

RewriteOptions MaxRedirects=20

和类似这样的东西到你的 apache conf 文件

and something like this to your apache conf file

LimitInternalRecursion 20

选择您需要允许的任意数量的递归(对)而不是20"(默认为 10).

Instead of "20" pick whatever number of recursions (pairs) you need to allow (the default is 10).

相关文章