带正则表达式的简单modrewrite规则

时间:2013-07-16 11:43:07

标签: mod-rewrite

这是我的网址

http://test.aDomain.com/x-61bff
http://test.aDomain.com/x-ssmJhs54as65d4
http://test.aDomain.com/x-115545454

我的重写规则

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

RewriteRule ^create-account create-account.php [L]
RewriteRule ^account account.php [L]
RewriteRule ^impress impress.php [L]


RewriteRule ^(x\-[a-zA-Z0-9]+) redirect.php?code=$1 [L]

结果是

http://test.aDomain.com/redirect.php 

但为什么呢?它应该是

http://test.aDomain.com/redirect.php?code=x-61bff

我不明白......任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

主要是因为你得到了正则表达式错误:

  1. 主机名和路径之间的/ 确实是路径的一部分(例如/x-61bff而不是x-61bff) 。但是,您的正则表达式在开头只匹配x,因此/x-61bff 永远匹配。

  2. 减号在方括号内只有特殊含义(例如[A-Z]);外面只是另一个角色,没有必要逃避它

  3. 所以你的重写规则应该是这样的:

    RewriteRule ^/(x-[a-zA-Z0-9]*) redirect.php?code=$1 [L]
    
相关问题