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

时间:2009-12-08 14:37:22

标签: php apache .htaccess mod-rewrite recursion

我一直在阅读一个previous solution的递归mod_rewrite问题,这与我正在尝试的类似,区别在于我通过index.php文件发送所有查询,所以不要不需要在查询中指定脚本。

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

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

到常规查询字符串:

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

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

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

有人能提供任何建议吗?

4 个答案:

答案 0 :(得分:2)

我从其他问题中复制了解决方案并将其修改为:

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

它几乎完全相同,除了在第一个规则中,第一个匹配是可选的,而在第二个规则中,匹配是在所有其他对匹配后剩下的任何内容。

对于奇数个参数,忽略第一个参数。

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

将这样的内容添加到.htaccess文件

RewriteOptions MaxRedirects=20

和你的apache conf文件中的类似内容

LimitInternalRecursion 20

而不是“20”选择你需要允许的任何数量的递归(对)(默认值为10)。

答案 1 :(得分:1)

我知道这是一个非常古老的主题。但由于这里发布的答案都没有对我有用,我想发布我的答案,以便访问者可以使用它,如果他们想要的话我会在这里回答:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$1=$2 [N,QSA,DPI]
RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI]

有关详细信息,请参阅MOD_REWRITE for URL variables, removing empty parameters and using more than 1field for the first and second variables

上的答案

答案 2 :(得分:0)

是...

取自这里的例子:

http://iirf.codeplex.com/sourcecontrol/changeset/view/62027?projectName=IIRF#981491

此规则集迭代地将一对URL路径段转换为查询字符串n = v段。

# handle case with no query string.  This rule fires the first time through,
# and converts the first pair of URL path segments to a n=v query string segment.  
RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)/([^\?]+)$ /$3?$1=$2

# handle the case where there is an existing query string, and more than one pair of 
# URL path segments remaining. This rule fires potentially multiple times.
RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)/([^\?]+)\?(.+)$ /$3?$4&$1=$2


# Handle the case with a query string, and exactly one pair of URL path segments. 
# This fires once (last).
# It fires when there is an even number of segments. 
RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)\?([^\?]+)$ /help.cfm?$3&$1=$2  [L]

# Handle the case with no query string, and exactly one pair of URL path segments. 
# This fires once (last).
RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)$ /help.cfm?$1=$2  [L]


# Handle the edge case, where there is an odd number of segments, which is invalid
# for these purposes. 
#
# This fires once, after all the other pairs have been parsed.  In fact the filter
# cannot know that there is an odd number of segments until it does all the matching.
# So at the end we see, we have one left over segment, and we 
# redirect to a 404 page.

RewriteRule ^/(?!index\.php)([^\/\?]+)\?([^\?]+)$ /ResourceNotFound.php  [L]

# Handle the edge case where there is only one segment. This is also an error
# in this ruleset.
RewriteRule ^/(?!index\.php)([^\/\?]+)$ /FoundOnlyOneSegment.php  [L]

这个规则集可能不是你想要的,但它说明了这种方法。它不是为mod_rewrite开发的,而是为IIRF开发的,后者是IIS的重写器。但是mod_rewrite的语法应该是相同的,所以这些规则应该可行。

我不知道mod_rewrite是否具有日志工具或命令行测试功能,但IIRF 执行,这样可以更轻松地查看各个规则的工作方式或集合的结果规则。

IIRF的迭代限制默认为10.有一种方法可以将该限制提高到30,这是非常高的,但仍然不是无限的。这意味着它不适用于“任意数量的参数”。它最多可以转换15对参数。我不知道mod_rewrite是否有类似的限制。

答案 3 :(得分:0)

请尝试以下规则:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)(/(.*))?$ /$4?$1=$2 [N,QSA]
RewriteRule ^$ index.php [L]

第一条规则是排除对现有文件的请求。第二条规则将完成工作并重写每个名称和值对。第三条规则是将最终URI重写为 index.php