为url重写添加一个例外

时间:2014-07-21 05:37:14

标签: php regex apache .htaccess mod-rewrite

我有以下.htaccess文件内容:

RewriteEngine On

RewriteCond %{THE_REQUEST} /index\.php\?p=((?![^&]*?edit)[^\s&]+) [NC]

RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

这会重写以下网址: http://domain.com/index.php?p=Contacts&ms=m5&l=en&

进入: http://domain.com/Contacts

它还会排除包含单词" edit"在变量p的末尾,例如: http://domain.com/index.php?p=Contactsedit&l=en

我想再添加一个条件来排除我的.htaccess文件,这也是为了排除包含单词" admin"的网址。在变量p中,例如: http://domain.com/index.php?p=admin

你们可以帮我修改病情或增加排除条件吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

修改你的第一个条件:

RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)((?![^&]*?edit)[^\s&]+) [NC]

(?!admin)是一个负面的预测,断言后面的内容不是admin

<强>参考