.htaccess漂亮的网址问题(mod_rewrite) [英] .htaccess pretty url problem (mod_rewrite)

查看:66
本文介绍了.htaccess漂亮的网址问题(mod_rewrite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,按类别列出产品。如果存在 _GET 变量,则在查询中使用它。我想使用漂亮的网址,例如: example / a / 1 / b / 2 / c / 3 / d / 4 成为 example /index.html?a=1&b=2&c=3&d=4

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

最多。 htaccess 的示例我只看到使用变量来替换 _GET 的值,但是我可以使用如下规则:

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]

它可以工作...但是,当我添加越来越长的 RewriteRules 时(例如& 17 = $ 18 ),它将停止工作。链中的最后一个变量根据先前的值变成某种数组(在上面,它会生成 index.html?a0 = a1& a3 = a4 )...

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


  • 是否有更好的方法?

  • 似乎效率低下?

  • .htaccess


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

谢谢!

推荐答案

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

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.

但是,对我来说,检查服务器的 REQUEST_URI / SCRIPT_NAME / PATH_INFO 变量,然后解析该变量以从中获取键值对网址。然后,您只需在 .htaccess 中保存此内容即可:

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]

然后您的脚本将负责其余的工作。我确实想知道,如果您有那个许多 GET 变量,如果将它们全部制成一个漂亮的,实际上是否更易读?网址?毕竟,如果您在URL中使用二十多个正斜杠,那么仅在此时传递一个普通的查询字符串就可以了。但是,这取决于您的应用程序以及用户如何使用这些URL进行交互,因此您可能有充分的理由希望这样做。

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.

这篇关于.htaccess漂亮的网址问题(mod_rewrite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆