.htaccess规则匹配新URL的一部分,并用作旧URL的查询字符串

发布于 2025-02-02 01:25:03 字数 370 浏览 3 评论 0原文

我正在尝试匹配URL的一部分,然后使用匹配的表达式将其附加到旧URL的查询字符串末尾。

RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1

我在.htaccess中都有以下 想要([^/]*)$出现在$ 1

本质上是如此:/lemay/29/Matches/将指向<代码> index.php?page = 1074&amp; loague = 29

有人可以告诉我我在做什么错吗? :)

I am trying to match part of a URL and then use the matched expression to append onto the end of a query string from the old URL.

I have the following line in .htaccess, once I've worked out what I'm doing wrong I'll be able to fix the rest so for now I will just focus on the following line:

RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1

I would like ([^/]*)$ to appear where $1 is

So essentially: /league/29/matches/ would point to index.php?page=1074&league=29

Can anyone please tell me what I am doing wrong? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我的奇迹 2025-02-09 01:25:03
 重写 ^leake/([ ^/]*)$/matches/? index.php?page_id = 1074&amp; legue = $ 1
 

$(串联锚定)在subpattern 之后导致正则表达式失败。您还应该在此处使用+量词(1个或更多)。您还缺少在正则末尾的串联锚点($)(否则thailing /?是多余的)。尽管您不应该在重写上真正使Taild Slash可选,因为它可能会为您打开重复的内容。 (您应该重定向以附加/删除落后的斜线以改用URL。)您还缺少l flag在Rewriterulelule上。

试试以下操作:

RewriteRule ^league/([^/]+)/matches/?$ index.php?page_id=1074&league=$1 [L]

尽管您只期望数字(如示例中),那么您应该与数字匹配,而不是任何东西。因此,以下内容可能“更”正确:

RewriteRule ^league/(\d+)/matches/$ index.php?page_id=1074&league=$1 [L]
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1

The $ (end-of-string anchor) after the subpattern ([^/]*)$, in the middle of the regex, does not make sense here and will cause the regex to fail. You should also be using the + quantifier here (1 or more). You are also missing the end-of-string anchor ($) at the end of the regex (otherwise the trailing /? is superfluous). Although you shouldn't really make the trailing slash optional on the rewrite as it potentially opens you up for duplicate content. (You should redirect to append/remove the trailing slash to canonicalise the URL instead.) You are also missing the L flag on the RewriteRule.

Try the following instead:

RewriteRule ^league/([^/]+)/matches/?$ index.php?page_id=1074&league=$1 [L]

Although, if you are expecting digits only (as in your example) then you should be matching digits, not anything. So, the following is perhaps "more" correct:

RewriteRule ^league/(\d+)/matches/$ index.php?page_id=1074&league=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文