htaccess - 如何不基于url模式重定向

时间:2014-10-06 16:51:43

标签: regex apache .htaccess mod-rewrite

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

# load index.php by default
DirectoryIndex index.php

RewriteBase /

# for all other requests load room.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(index|room)\.php).+)$ room.php?u=$1 [L,NC]

我的htaccess目前会将任何人重定向到room.php,如果网址包含除http://www.example.com/anythingelse等根目录之外的其他内容,如果不包含,则会将其简单地指向index.php

但我需要能够不为特定模式包含此重定向,以便我可以在特定文件夹中创建内容。例如

http://www.example.com/1
http://www.example.com/2
http://www.example.com/3
http://www.example.com/4

这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以在正则表达式模式中添加这些排除项:

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

# load index.php by default
DirectoryIndex index.php

RewriteBase /

# for all other requests load room.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(1|2|3|4)
RewriteRule ^((?!(index|room)\.php).+)$ room.php?u=$1 [L,NC]