htaccess中有例外的规则

时间:2013-12-10 16:03:08

标签: regex apache .htaccess mod-rewrite

我的规则是从http://localhost/username重定向到http://localhost/profile_redirect.php?user=username

htaccess规则是

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [L]

# when user name != directory 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ profile_redirect.php?user=$1 [L,QSA]

# when user name == directory 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^.]+)/$ profile_redirect.php?user=$1 [L,QSA]

是否可以为此规则设置例外情况?例如,我有一个包含网址http://localhost/members的网页,因此在使用该网址时,由于.htaccess规则,它会重定向到http://localhost/profile_redirect.php?user=members,这不应该发生。

我只有3个这样的例外。

1 个答案:

答案 0 :(得分:2)

你的规则如下:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [L]

# ignore these paths for further rewriting
RewriteRule ^(members|exception1|exception2)(/|$) - [NC,L]

# when user name != directory 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ profile_redirect.php?user=$1 [L,QSA]

# when user name == directory 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^.]+)/$ profile_redirect.php?user=$1 [L,QSA]