url 递归重写
我正在尝试将网址从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mod rewrite 将一遍又一遍地通过重写引擎运行 URI,直到 URI 在通过重写引擎之前和之后相同。这是发生的事情:
^((.+)\.(.+))$
匹配,URI 重写为 my.php (带有一些查询字符串d = test.com)^((.+)\.(.+))$
匹配,URI 重写为 my.php (使用一些查询字符串d = my.php)你的结果是
/my.php?d=my.php
您需要添加一个条件,以便 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:
^((.+)\.(.+))$
matches, URI rewritten to my.php (with some query string d = test.com)^((.+)\.(.+))$
matches, URI rewritten to my.php (with some query string d = my.php)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
这对我有用:
效果不是递归应用规则(这不会发生),它只是不匹配。
顺便说一句,如果你想让引擎停在特定的行,你可以添加[L]。
测试重写规则的一个简单方法是此网站。
This works for me:
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.