【发布时间】:2016-03-05 15:29:10
【问题描述】:
我注意到 htaccess 中的 [OR] 存在我不理解的问题。我想要做的是将每个不存在的文件和每个 .php 文件(而不是 /index.php)文件重定向到 /404/。据我了解,以下两个块应该做同样的事情:
# .htaccess 1
RewriteEngine on
# cond 1
RewriteCond %{REQUEST_FILENAME} !-f [OR]
# cond 2
RewriteCond %{REQUEST_FILENAME} !index\.php$
# cond 3
RewriteCond %{REQUEST_URI} \.php$
# rule 1
RewriteRule [^\.]*\.[a-zA-Z0-9]+$ /404/ [L]
# rule 2
RewriteRule ^(([^/\.]+)/?)*?$ /index.php [L]
还有这个:
# .htaccess 2
RewriteEngine on
# cond 1
RewriteCond %{REQUEST_FILENAME} !-f
# rule 1
RewriteRule [^\.]*\.[a-zA-Z0-9]+$ /404/ [L]
# cond 2
RewriteCond %{REQUEST_FILENAME} !index\.php$
# cond 3
RewriteCond %{REQUEST_URI} \.php$
# rule 2
RewriteRule [^\.]*\.[a-zA-Z0-9]+$ /404/ [L]
# rule 3
RewriteRule ^(([^/\.]+)/?)*?$ /index.php [L]
然而,只有第二个块做我想要的。例如,第一个 .htaccess - 无法重定向不存在的文件 a.pdf,而是显示有关未找到文件的默认服务器消息。
谁能给我解释一下?
【问题讨论】:
-
对于任何不存在文件的 URL,例如:
/a.pdf -
这就是问题所在——据我了解,这两个块都应该做同样的事情。然而,第二个块中的行为是正确的
-
请创建 index.php 文件,其中包含“A”。首先使用.htaccess。对于
/a.pdfURL,您将获得默认的“未找到”服务器响应。使用第二个 .htaccess。你会得到“A”,因为/a.pdf将解析为/404/,而/index.php将解析为/index.php。现在 - 为什么行为不一样? -
因为还有其他规则,我希望不存在的文件和每个 php 文件都遵循已经建立的已知行为。
-
为什么?我提供了复制问题的代码。我要问的只是解释为什么行为不一样。
标签: .htaccess