php - htaccess URL 重写最大参数长度

标签 php .htaccess url-rewriting

我正在构建一个支持以下功能的迷你框架:

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

用作 Bootstrap :

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 个)。这可能会超出您的路由规则。


编辑:

becuase on the forth block this case doesnt hold up: 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 的一部分,它需要特殊条件。

关于php - htaccess URL 重写最大参数长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778474/

相关文章:

javascript - 通过 HTML 按钮传递 PHP 函数(带参数)

php - 我无法在 php 文件中从应用程序接收注册信息,无法将该信息插入 MSQL 数据库

java - 在Java中解析requestUrl

递归子文件夹链接的 .htaccess 规则

jsf - 使用 OcpSoft rewrite Join.path 规则重写 URL

php - 删除操作不适用于 Yii 中的相关模型

php - 更改 php.ini 对我的 EC2 实例的 PHP 配置没有影响

.htaccess - htaccess/mod_rewrite/SSL |如果 : http off and (basket or order), 301 https,否则 301 http

.htaccess 仅适用于特定文件夹脚本

php - 添加类型应用程序/x-httpd-php .html