URL 中的 & 符号问题

发布于 2024-12-18 03:55:59 字数 1315 浏览 2 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

心在旅行 2024-12-25 03:55:59

1. 我不太喜欢在网址中包含空格和其他特殊字符。我不知道您的网站是否可以,但是

vendors/London City/cat-DJ & Entertainment/keywords

您应该使用以下 URL,而不是这种 URL:

vendors/london-city/cat-dj-and-entertainment/keywords

为此,当然,您必须在数据库中执行一些额外的转换/查找才能将 london 转换为 london -city 返回到 London Citydj-and-entertainment 返回到 DJ &娱乐。这可以通过将这些“从-到”对存储在数据库中来完成。

2.无论如何——规则的顺序很重要。因此,您应该从更具体的规则开始,最后以更通用的规则结束。

另外,(.+) 模式过于宽泛,因为它可以匹配 hello 以及 hello/pink/kitten。为了确保您始终只抓取一个部分(/ 之间的 URL 部分),请使用 ([^/]+) 模式 - 这将解决以下方面之一你的“问题#2”。

因此,请尝试这些优化的规则(每个规则将匹配带或不带尾部斜杠的 URL):

RewriteRule ^vendors/([^/]+)/cat-([^/]+)/([^/]+)/?$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule ^vendors/([^/]+)/cat-([^/]+)/?$ vendors.php?location=$1&category=$2 [L]
RewriteRule ^vendors/([^/]+)/([^/]+)/?$ vendors.php?location=$1&freetext=$2 [L] 

此外,我没有得到“类别”与“&”符号的值
网址中给出。我只得到分号。可以是什么
原因?

我旁边当前没有运行 Apache 盒子,因此现在无法检查它,但请尝试在 LB 或 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

vendors/London City/cat-DJ & Entertainment/keywords

you should have this one:

vendors/london-city/cat-dj-and-entertainment/keywords

For that, of course, you will have to perform some additional transformations / lookups in your database to convert london-city back to London City and dj-and-entertainment back to DJ & 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 match hello as well as hello/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):

RewriteRule ^vendors/([^/]+)/cat-([^/]+)/([^/]+)/?$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule ^vendors/([^/]+)/cat-([^/]+)/?$ vendors.php?location=$1&category=$2 [L]
RewriteRule ^vendors/([^/]+)/([^/]+)/?$ vendors.php?location=$1&freetext=$2 [L] 

Also I'm not getting the value of 'category' with the Ampersand as
given in the url. I am getting only semi-colon. What can be the
reason?

I do not have Apache box currently running next to me, so cannot check it right now, but try adding B or NE flag next to the L flag (e.g. [L,B]) -- one of them should help:

蒲公英的约定 2024-12-25 03:55:59

来自文档

定义这些规则的顺序很重要 - 这是它们在运行时应用的顺序。

From the docs:

The order in which these rules are defined is important - this is the order in which they will be applied at run-time.

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