结合 htaccess 中的重写条件进行重定向

发布于 2024-08-24 21:28:08 字数 393 浏览 5 评论 0原文

我有一个 cms 驱动的网站,无法更改代码。我想要完成的是仅使用 apaches mod-rewrite 引擎创建友好的 url。

问题是我正在创建一个无限循环,因为我首先将原始网址 (index.php?id=21) 重定向到友好网址 (/Friendly/),然后将 '/Friendly' 部分重写回 'id= 21'

我知道应该有一个额外的条件或参数来避免在这种情况下循环,但我无法获得可行的解决方案之一。

这是代码:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]

I've got a cms-driven website, with no option to change the code. What I want to accomplish is creating friendly url's, using only apaches mod-rewrite engine.

The problem is I'm creating an infinite loop, because I first redirect the original url (index.php?id=21) to a friendly one (/friendly/) and then rewrite the '/friendly' part back to 'id=21'

I know there should be an extra condition or parameter to avoid looping in this case, but I can´t get one of the possible solutions to work.

Here´s the code:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]

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

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

发布评论

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

评论(2

欢你一世 2024-08-31 21:28:09

我猜您正在两个方向重写以确保旧链接重定向到新的友好网址?

您可以向所有“友好”重写添加一个虚拟参数,以便它们与其他规则不匹配:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21&dummy=1 [L]

I'm guessing you are rewriting in both directions to ensure that old links are redirected to the new friendly urls?

You could just add a dummy parameter to all your "friendly" rewrites so that they don't match the other rule:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21&dummy=1 [L]
夕嗳→ 2024-08-31 21:28:08

您需要查看 请求行 < em>THE_REQUEST 查看最初请求的内容:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?id=21\  [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]

You need to look at the request line in THE_REQUEST to see what originally has been requested:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?id=21\  [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

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