url 递归重写

发布于 2024-12-28 08:51:53 字数 354 浏览 1 评论 0原文

我正在尝试将网址从 www.xxx.com/test.com 重写为 www.xxx.com/my.php?d=test.com 使用以下指令:

        Options Indexes FollowSymLinks
        RewriteEngine On

        RewriteRule ^((.+)\.(.+))$ my.php?d=$1

这不起作用,例如,url 是 www.xxx.com/test.com 似乎它被重写为 www.xxx.com/my.php?d=test.com 然后重写为 www.xx.com/my.php?d=my.php 或类似的内容。这是否意味着该模式正在递归应用? 如何修复正则表达式?

I am trying to rewrite url from www.xxx.com/test.com to www.xxx.com/my.php?d=test.com
using the following directive:

        Options Indexes FollowSymLinks
        RewriteEngine On

        RewriteRule ^((.+)\.(.+))$ my.php?d=$1

this is not working, for example, the url is www.xxx.com/test.com
it seems like it gets rewrite to www.xxx.com/my.php?d=test.com
then gets rewrite to www.xx.com/my.php?d=my.php or something like that. does this mean the pattern is getting applied recursively??
how do I fix the regex?

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

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

发布评论

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

评论(2

柳絮泡泡 2025-01-04 08:51:53

Mod rewrite 将一遍又一遍地通过重写引擎运行 URI,直到 URI 在通过重写引擎之前和之后相同。这是发生的事情:

  1. URI 与 test.com
  2. 规则 ^((.+)\.(.+))$ 匹配,URI 重写为 my.php (带有一些查询字符串d = test.com
  3. 比较:test.commy不同.php,通过重写引擎URI运行回
  4. URI是 my.php
  5. 规则 ^((.+)\.(.+))$ 匹配,URI 重写为 my.php (使用一些查询字符串d = my.php
  6. 比较:my.phpmy.php相同,在 URI 匹配之前和之后,停止写入

你的结果是/my.php?d=my.php

您需要添加一个条件,以便 my.php 不会应用该规则。在重写规则之前添加此内容

RewriteCond %{REQUEST_URI} !^/my.php

Mod rewrite will run a URI through the rewrite engine over and over until the URI is the same before and after it goes through the rewrite engine. This is what's happening:

  1. URI is test.com
  2. rule ^((.+)\.(.+))$ matches, URI rewritten to my.php (with some query string d = test.com)
  3. compare: test.com is not the same as my.php, run the URI back through the rewrite engine
  4. URI is my.php
  5. rule ^((.+)\.(.+))$ matches, URI rewritten to my.php (with some query string d = my.php)
  6. compare: my.php is the same as my.php, before and after URI match, stop writing

Your result is /my.php?d=my.php

You need to add a condition so my.php doesn't get the rule applied. Add this before your rewriterule

RewriteCond %{REQUEST_URI} !^/my.php
折戟 2025-01-04 08:51:53

这对我有用:

RewriteRule ^(.+)\.(.+)$ my.php?d=$1.$2

效果不是递归应用规则(这不会发生),它只是不匹配。

顺便说一句,如果你想让引擎停在特定的行,你可以添加[L]。

测试重写规则的一个简单方法是此网站

This works for me:

RewriteRule ^(.+)\.(.+)$ my.php?d=$1.$2

The effect is not that the rule is applied recursively (that doesn't happen), it simply didn't match.

BTW, if you want to make the engine stop at a specific line, you can add [L].

An easy way to test rewrite rule is this website.

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