Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
181 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...