Apache RewriteCond和RewriteRule跳过现有文件和目录

时间:2013-12-15 17:27:14

标签: apache .htaccess mod-rewrite virtualhost url-redirection

我的本​​地网站有以下目录结构。

mysite
|_ css
|_ images
    |_ logo.png
|_ js
|_ app
    |_ index.php
|_ .htaccess

我正在尝试将Apache URL重写为重定向到http://mysite.dev/app/index.php以查找不存在的目录。 例如,http://mysite.dev/en/home将被重写为http://mysite.dev/app/index.php

我在.htaccess文件中尝试了这个重写规则:

Options -Indexes
DirectoryIndex index.php index.html index.htm

<IfModule mod_rewrite.c>
   RewriteEngine on

   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ app/index.php [L]   
</IfModule>

但是它被重写为所有现有目录和没有现有目录。 也就是说,当我浏览http://mysite.dev/mysite/images/logo.png时,它会被重写为http://mysite.dev/mysite/app/index.php

我在两条RewriteCond行上方添加了一行,但它没有用。

RewriteRule ^(images|css|js) - [NC,L]

如果存在任何文件或目录,我的目标是跳过规则。 如何为此目的撰写RewriteCondRewriteRule

[编辑]
我发现了这个问题。这是因为我为我的网站添加了虚拟主机。我在httpd-vhost.conf中有以下几行代码:

<VirtualHost mysite.dev>
    DocumentRoot "C:/xampp/htdocs/mysite"
    ServerName mysite.dev
    ServerAlias mysite.dev
    <Directory "C:/xampp/htdocs/mysite">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

当我浏览http://mysite.dev/images/logo.png时,RewriteCond无法正常工作 但当我删除虚拟主机并浏览http://localhost/mysite/images/logo.png时,它工作正常,我正确地看到了图像。

我不确定虚拟主机和RewriteCond的问题和冲突是什么。

1 个答案:

答案 0 :(得分:0)

在您的虚拟主机指令中,您需要AllowOverride directive

<VirtualHost mysite.dev>
    DocumentRoot "C:/xampp/htdocs/mysite"
    ServerName mysite.dev
    ServerAlias mysite.dev
    <Directory "C:/xampp/htdocs/mysite">
        AllowOverride FileInfo

        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>