问题
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?
回答1:
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:
- URI is test.com
- rule
^((.+)\.(.+))$
matches, URI rewritten to my.php (with some query string d = test.com) - compare: test.com is not the same as my.php, run the URI back through the rewrite engine
- URI is my.php
- rule
^((.+)\.(.+))$
matches, URI rewritten to my.php (with some query string d = my.php) - 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
回答2:
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.
来源:https://stackoverflow.com/questions/8953895/url-rewrite-recursively