url rewrite recursively

岁酱吖の 提交于 2019-12-13 08:34:31

问题


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:

  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



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!