如何从Apache URL重写条件中排除子目录

时间:2014-01-27 16:34:17

标签: apache .htaccess mod-rewrite

我将以下重写条件应用于Apache2 webserver的根目录(在apache.conf中)

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{SERVER_ADDR} !=127.0.0.1
    RewriteCond %{SERVER_ADDR} !=::1
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>

我有一个子目录www.domain.com/subdir,我不希望这些重写适用于。如何从这些选项中排除subdir

2 个答案:

答案 0 :(得分:1)

实际上,您只需要提升跳过文件/目录的最后一条规则。试试这段代码:

Options +FollowSymlinks
RewriteEngine On

# if request is not for a file/directory 
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
# then skip from rewrites
RewriteRule ^ - [L]

# add www to hostname
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# forward all the requests to `/index.php
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

答案 1 :(得分:0)

在此行之后

RewriteEngine On

添加:

RewriteRule ^/subdir/ - [L]

RewriteCond %{THE_REQUEST} \ /+subdir/
RewriteRule ^ - [L]