URL 中的 & 符号问题
我有一个 php 页面,它创建如下 URL:
vendors/London City/cat-DJ & Entertainment/keywords
我的 .htaccess 重定向如下所示
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3 [L]
问题 1 是: 在vendors.php 文件中,我只得到“DJ ; Entertainment”作为类别。缺少 & 符号。
问题 2 是:我完整的 .htaccess 文件如下所示...定义了 6 条规则。
RewriteRule vendors/(.+)/(.+)/$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/(.+)$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3[L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3[L]
为什么 URL vendors/London City/cat-DJ & Entertainment/keywords 与规则 3 或 4 匹配并重定向到 sellers.php?location=$1&category=$2 ?
.htaccess 是否从上到下一一处理规则?
我通过将规则 5 和规则 6 放在其他规则的顶部解决了这个问题。我做了正确的修复吗?
I have a php page which creates URL like:
vendors/London City/cat-DJ & Entertainment/keywords
which my .htaccess redirects as shown below
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3 [L]
problem 1 is : in the vendors.php file, I am getting only "DJ ; Entertainment" as category. The ampersand is missing.
Problem 2 is : My complete .htaccess file is shown below... 6 rules are defined.
RewriteRule vendors/(.+)/(.+)/$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/(.+)$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3[L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3[L]
Why the URL vendors/London City/cat-DJ & Entertainment/keywords is matching with rule 3 or 4 and redirecting to vendors.php?location=$1&category=$2 ?
Does .htaccess Process the rules from top to beginning one by one?
I had solved the problem by putting the rules 5 and 6 at the top of other rules. Did I make the correct fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1. 我不太喜欢在网址中包含空格和其他特殊字符。我不知道您的网站是否可以,但是
您应该使用以下 URL,而不是这种 URL:
为此,当然,您必须在数据库中执行一些额外的转换/查找才能将 london 转换为 london -city 返回到
London City
,dj-and-entertainment
返回到DJ &娱乐
。这可以通过将这些“从-到”对存储在数据库中来完成。2.无论如何——规则的顺序很重要。因此,您应该从更具体的规则开始,最后以更通用的规则结束。
另外,
(.+)
模式过于宽泛,因为它可以匹配hello
以及hello/pink/kitten
。为了确保您始终只抓取一个部分(/
之间的 URL 部分),请使用([^/]+)
模式 - 这将解决以下方面之一你的“问题#2”。因此,请尝试这些优化的规则(每个规则将匹配带或不带尾部斜杠的 URL):
我旁边当前没有运行 Apache 盒子,因此现在无法检查它,但请尝试在
L
B 或NE
标志> 标志(例如[L,B]
)——其中之一应该有帮助:1. I don't really like the idea of having spaces and other special characters in the URLs. I don't know if it's possible with your site, but instead of this kind of URL
you should have this one:
For that, of course, you will have to perform some additional transformations / lookups in your database to convert
london-city
back toLondon City
anddj-and-entertainment
back toDJ & Entertainment
. This can be done by storing these "from-to" pairs in database.2. In any case -- order of rules matters. Therefore you should start with more specific rules and end up with more generic rules.
Also -- the
(.+)
pattern is a way too broad as it can matchhello
as well ashello/pink/kitten
. To ensure that you always grab only one section (part of URL between/
) use([^/]+)
pattern instead -- this will address one of the aspects of your "prob #2".Therefore, try these optimized rules (each rule will match the URL with and without trailing slash):
I do not have Apache box currently running next to me, so cannot check it right now, but try adding
B
orNE
flag next to theL
flag (e.g.[L,B]
) -- one of them should help:来自文档:
From the docs: