.htaccess 漂亮的 url 问题 (mod_rewrite)
我有一个按类别列出产品的目录。如果 _GET
变量存在,则在查询中使用它。我想使用“漂亮的网址”,例如: example/a/1/b/2/c/3/d/4
变为 example/index.html?a=1& b=2&c=3&d=4
我看到的大多数 .htaccess
示例仅使用变量来替换 _GET
值,但我可以使用类似的规则这个:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.html?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.html?$1=$2&$3=$4 [L]
RewriteRule ([^/]+)/([^/]+)$ index.html?$1=$2 [L]
它有效...但是,当我添加越来越长的 RewriteRules
(例如 &17=$18
)时,它停止工作。链中的最后一个变量根据较早的值变成某种数组(在上面它将构建 index.html?a0=a1&a3=a4
)...
- 有没有更好的方法做这个吗?
- 看起来效率很低?
.htaccess
中的变量数量是否有限制- 规则可以有多长?
谢谢!
I have a directory that lists products by categories. if a _GET
variable exists, it is used in a query. I would like to use "pretty url's", like: example/a/1/b/2/c/3/d/4
becomes example/index.html?a=1&b=2&c=3&d=4
most .htaccess
examples I see only use variables to replace the _GET
values, but I can use rules like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.html?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.html?$1=$2&$3=$4 [L]
RewriteRule ([^/]+)/([^/]+)$ index.html?$1=$2 [L]
And it works... However, the when I add longer and longer RewriteRules
(like out to &17=$18
), it stops working. The last variables in the chain turn into some sort of array based on earlier values (in above it would build index.html?a0=a1&a3=a4
)...
- Is there a better way to do this?
- It seems inefficient?
- Is there a limit to the number of variables in
.htaccess
- How long a rule can be?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mod_rewrite 最多仅支持
$9
和%9
。我建议您修改脚本以使用
$_SERVER['PATH_INFO']
,或者使用RewriteMap
调用脚本将路径转换为查询字符串。mod_rewrite only supports up to
$9
and%9
.I recommend you either modify your script to use
$_SERVER['PATH_INFO']
, or you useRewriteMap
to invoke a script to transform the path into a querystring.mod_rewrite
只允许您有十个反向引用,其中之一是整个可匹配部分(最终只剩下九个可定义的捕获组),因此您肯定受到限制。但是,对我来说,检查脚本文件中服务器的
REQUEST_URI
/SCRIPT_NAME
/PATH_INFO
变量并将其解析为更有意义从 URL 中获取键值对。然后,您只需将其放在.htaccess
中即可:然后您的脚本将处理其余的事情。不过,我确实想知道,如果您有那么多
GET
变量,那么如果将它们全部制作成“漂亮”的 URL,实际上是否会更具可读性?毕竟,如果 URL 中有二十几个正斜杠,那么此时只需传递一个普通的查询字符串就可以了。但这取决于您的应用程序以及用户如何与这些 URL 交互,因此您可能有充分的理由想要这样做。mod_rewrite
only allows for you to have ten back-references, one of which is the whole matchable part (which ends up leaving you with only nine definable capture groups), so you're definitely limited by that.However, to me it would make much more sense to examine the server's
REQUEST_URI
/SCRIPT_NAME
/PATH_INFO
variable in your script file, and parse that to get the key-value pairs from the URL. Then, you'd simply have this in your.htaccess
:And then your script would take care of the rest. I do have to wonder though, if you have that many
GET
variables, is it actually more readable if they're all made into a "pretty" URL? After all, if you have twenty-some forward slashes in the URL, you may be equally well off just passing a normal query string at that point. It depends on your application though and how users interface with these URLs, so you may have good reason for wanting to do it this way.