htaccess URL重写最大参数长度

时间:2012-08-02 13:29:10

标签: php .htaccess url-rewriting

我正在建立一个支持的迷你框架:

mysite.com/template/
mysite.com/template/action/
mysite.com/template/action/model/

用作引导程序:

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* /index.php

但是对于任何请求:

mysite.com/template/action/model/**extra/stuff/should/vanish/**

这样最大的URL大小总会丢掉额外的东西:

mysite.com/template/action/model/

1 个答案:

答案 0 :(得分:4)

你的意思是这样的吗?

# if more than 3 nodes deep          v--- the extra stuff should vanish
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/.+ /$1/$2/$3/? [R=301,L]

这会重定向浏览器,以便在超过3时仅保留3个节点。这可能会超出您的路由规则。


编辑:

  

因为这个案子没有阻止第四块:site.com/a/b/c/?d

您需要一个特定的规则来匹配查询字符串,这超出了您在RewriteRule的表达式中可以执行的操作。您可以在上述规则之前或之后加上这些规则:

# Check if there's a query string
RewriteCond %{QUERY_STRING} !^$
# if there are exactly 3 nodes, then remove the query string
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2/$3/? [R=301,L]

查询字符串( ? 之后的所有内容)不是重写引擎中使用的URI的一部分,它需要特殊条件。