简体   繁体   English

.htaccess漂亮的网址问题(mod_rewrite)

[英].htaccess pretty url problem (mod_rewrite)

I have a directory that lists products by categories. 我有一个目录,按类别列出产品。 if a _GET variable exists, it is used in a query. 如果存在_GET变量,则在查询中使用它。 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 我想使用“漂亮的网址”,例如: example/a/1/b/2/c/3/d/4变成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: 我看到的大多数.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]

And it works... However, the when I add longer and longer RewriteRules (like out to &17=$18 ), it stops working. 而且它可以工作...但是,当我添加越来越长的RewriteRules (例如&17=$18 )时,它将停止工作。 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 )... 链中的最后一个变量根据较早的值变成某种数组(在上面,它将建立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 .htaccess的变量数量是否有限制
  • How long a rule can be? 一条规则可以持续多久?

Thanks! 谢谢!

mod_rewrite only supports up to $9 and %9 . mod_rewrite仅支持$9%9

I recommend you either modify your script to use $_SERVER['PATH_INFO'] , or you use RewriteMap to invoke a script to transform the path into a querystring. 我建议您修改脚本以使用$_SERVER['PATH_INFO'] ,或使用RewriteMap调用脚本以将路径转换为查询字符串。

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. mod_rewrite仅允许您具有十个反向引用,其中之一是整个可匹配部分(最终只剩下九个可定义的捕获组),因此您肯定会受到限制。

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. 但是,对我而言,检查脚本文件中服务器的REQUEST_URI / SCRIPT_NAME / PATH_INFO变量,并对其进行解析以从URL中获取键值对将更为有意义。 Then, you'd simply have this in your .htaccess : 然后,您只需在.htaccess

RewriteRule On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]

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? 不过,我确实想知道,如果您有那么多的GET变量,如果将它们全部制成“漂亮的” 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. 毕竟,如果您在URL中使用二十多个正斜杠,那么仅在此时传递一个普通的查询字符串就可以了。 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. 但是,这取决于您的应用程序以及用户如何使用这些URL进行交互,因此您可能有充分的理由希望这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM