【问题标题】:How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?如何强制用于路由的 .htaccess 不路由 .css、.js、.jpg 等文件?
【发布时间】:2013-06-10 15:43:09
【问题描述】:

我在一个站点的 子目录 中有以下 .htaccess 文件,它允许我将所有 URL 路由到 index.php 以便我可以解析它们。

但是,它不允许使用网站所需的标准文件,例如css、javascript、png等。

我需要改变什么(我假设在第四行)以允许这些文件,这样它们就不会被路由到 index.php?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|png|jpg|gif|robots\.txt)
RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php

【问题讨论】:

  • 因为你有 !-f 规则 - 如果请求被重定向,它与磁盘上的文件不匹配,即无论如何,它是 4oh4。
  • 您是否需要从主 .htaccess 文件中继承任何内容(假设根文件夹中也有一个),还是希望这个覆盖另一个?
  • @AbsoluteZERO,如果可能的话,这个 .htaccess 文件应该覆盖它上面的任何东西。

标签: php .htaccess


【解决方案1】:

我注意到了。您使用的是正斜杠而不是问号...参数重定向通常如下所示:

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

这应该可以自己工作,因为这些文件中的任何一个*应该*是真实文件。

ErrorDocument 404 /index.php    

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

要让网站忽略特定的扩展名,您可以添加一个条件来忽略大小写,并且只检查请求中文件名的结尾:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

如果你想忽略一个文件夹,那么你可以添加:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(public|css)
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

【讨论】:

【解决方案2】:

最简单的方法是在规则的早期明确忽略它们:

RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteRule ^(index\.php|robots\.txt)$ - [L]

这可以避免使用 RewriteCond 将它们带到任何地方。

根据您的选择,在执行此操作之前检查文件是否存在:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(index\.php|robots\.txt)$ - [L]

(请注意,文件检查会产生额外的磁盘访问。)

【讨论】:

    【解决方案3】:

    你有正确的想法,调整第四行。 ^ 表示各种匹配字符串必须位于行首。如果您不关心这些文件中的任何位置,您可以删除^。这将避免重写 *.css、*.js 等;但也不会重写publicideas.html。

    如果你想只限制后缀,试试这个:

    RewriteCond $1 !^(index\.php|public|.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.gif|robots\.txt)$
    

    这表示要匹配开头的任何内容,然后是 .,然后是后缀。 $ 表示最后匹配这些(没有下文)。

    我不确定public,所以我留下了它(这意味着完全公开,没有其他内容 - 可能不是您的意思,但您可以在之前或之后添加*,或两者兼而有之)。

    【讨论】:

    • 当我将第四行替换为上面的行时,我在所有页面上都收到错误 500,即使我将第一行更改为 "RewriteCond $1 !^(index\.php|*\.css )$" 我收到错误 500。
    • @EdwardTanguay:看我对feeela 的回答和我自己的回答。
    【解决方案4】:

    RewriteCond %{REQUEST_FILENAME} !-f 一个人就足够了。

    另一个选项是不从重写中排除特定文件。 来自 TYPO3 包:

    # Stop rewrite processing, if we are in the typo3/ directory.
    # For httpd.conf, use this line instead of the next one:
    # RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
    RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
    

    此规则应出现在您实际重写之前。应该是

    RewriteRule ^(public/|*\.css|*\.js|*\.png|*\.jpg|*\.gif|robots\.txt) - [L]
    

    在你的情况下。

    【讨论】:

    • @Denis 你能告诉我是哪一个吗?我想我累了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多