Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

regex - url rewrite recursively

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...