【问题标题】:Ignore Rewrite Rule for images folder忽略图像文件夹的重写规则
【发布时间】:2011-12-23 23:10:26
【问题描述】:

我正在处理的网站上有一个 .htaccess 文件,该文件将 URL 从 mydomain.com/sub/folder/ 重写为 mydomain.com?index.php?controller=sub&view=folder

不幸的是,我编写它的方式意味着我无法再访问图像、样式表和其他链接文件。谁能告诉我如何最好地从重写规则中排除特定目录/ URL 请求?

抱歉,如果这是一个新手问题,我仍在思考这个 mod 重写的东西!

.htaccess 文件如下所示:

重写引擎开启 RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?Controller=$1 重写规则 ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-_]+)/? index.php?Controller=$1&View=$2

【问题讨论】:

  • 你所有的图片和css都是用相对路径还是绝对路径链接的?

标签: apache .htaccess


【解决方案1】:

如果您的图像在mydomain.com/images 中并且您使用页面mydomain.com/sub/folder/ 上的相对链接链接到它们,则浏览器将尝试通过mydomain.com/sub/folder/images/i.gif 访问图像。但是如果您将链接更改为绝对链接,浏览器将正确尝试加载mydomain.com/images/i.gif。但是,RewriteRule 会将其更改为:mydomain.com/index/php?Controller=images&View=i.gif。为避免这种情况,您需要添加一些 RewriteConds:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]+)\/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\/? index.php?Controller=$1&View=$2

因此,在尝试访问现有文件/目录时,不要重写 index.php。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2018-11-13
    • 1970-01-01
    • 2020-06-19
    • 2017-07-25
    • 2011-06-14
    相关资源
    最近更新 更多