htaccess URL重写最大参数长度


htaccess URL rewriting max parameter length

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

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/

你的意思是这样的吗?

# 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的一部分,它需要一个特殊条件。