【问题标题】:Apache RewriteCond and RewriteRule to skip for the existing files and directoriesApache RewriteCond 和 RewriteRule 跳过现有文件和目录
【发布时间】:2014-01-03 01:13:52
【问题描述】:

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

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有什么问题和冲突。

【问题讨论】:

  • 你的 htaccess 文件在哪里?
  • 您确定 images/logo.png 对 Apache 可见吗?如果不存在,Apache 会将其视为不存在。如果您删除您的 RewriteRule,您的主机可以为mydomain.com/images/logo.png 提供服务吗?
  • @Sithu:cssimages 是真实目录还是一些符号链接。
  • cssimagesjs 是真实目录,images/logo.png 对 Apache 也是可见的。我必须检查 apache 配置或 Windows 主机配置,因为它在另一台 PC 上完美运行,甚至没有 RewriteRule ^(images|css|js)

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


【解决方案1】:

在您的虚拟主机指令中,您需要有一个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>

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2020-07-21
    • 2010-11-16
    • 2012-04-19
    相关资源
    最近更新 更多