.htaccess 漂亮的 url 问题 (mod_rewrite)

发布于 2024-09-07 01:23:54 字数 845 浏览 1 评论 0原文

我有一个按类别列出产品的目录。如果 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-09-14 01:23:54

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 use RewriteMap to invoke a script to transform the path into a querystring.

阳光下的泡沫是彩色的 2024-09-14 01:23:54

mod_rewrite 只允许您有十个反向引用,其中之一是整个可匹配部分(最终只剩下九个可定义的捕获组),因此您肯定受到限制。

但是,对我来说,检查脚本文件中服务器的 REQUEST_URI/SCRIPT_NAME/PATH_INFO 变量并将其解析为更有意义从 URL 中获取键值对。然后,您只需将其放在 .htaccess 中即可:

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

然后您的脚本将处理其余的事情。不过,我确实想知道,如果您有那么多 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:

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? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文