.htaccess 检查两个位置的文件,否则抛出 404

发布于 2024-11-11 04:12:16 字数 1196 浏览 0 评论 0原文

按照我的网站当前的结构方式,实际网站位于其自己的单独文件夹中。我的意思是:

 /projects
 /files
 /pictures
 /tools
 /school
 /~webroot
 .htaccess

这使得文件系统更容易管理和导航。这是一种利用此功能的简单方法,无需让每个人导航到 http://domain.com/~webroot/,并且仍然允许他们访问文件等,例如 http://domain。 com/projects/,就是使用我下面编写的htaccess来检查真实根目录和~webroot目录中的文件。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]

但是,如果该文件在任何地方都不存在(root 或 ~webroot),则会抛出 HTTP 500 错误而不是 HTTP 404。为了显示我的 404 错误页面,我必须改用这些行:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /404.shtml [B,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^/~webroot
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]

这就是全部 < em>相当混乱,而且这只是一个技巧,实际上并不会抛出 HTTP 404,这使得我的统计应用程序无法记录 404。有没有办法:检查文件是否存在于 root 中,如果不存在,则检查文件是否存在于 ~webroot 中,如果不存在,则抛出真正的 HTTP 404 错误?

我也正确定义了所有 ErrorDocument

The way my site is currently structured, the actual site is in its own separate folder. Here's what I mean:

 /projects
 /files
 /pictures
 /tools
 /school
 /~webroot
 .htaccess

This makes the file-system much easier to manage and navigate. An easy way to utilize this, without having everyone navigate to http://domain.com/~webroot/, and still allow them to access files and such like http://domain.com/projects/, is to use the htaccess I wrote below to check for files in both the real root, and ~webroot directories.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]

However, if the file doesn't exist anywhere (root or ~webroot), an HTTP 500 error is thrown instead of an HTTP 404. In order to display my 404 error page, I have to instead use these lines:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /404.shtml [B,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^/~webroot
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]

This is all quite messy, and it's only a trick and doesn't actually throw an HTTP 404, which keeps 404s from being documented using my statistics application. Is there a way to: Check if file exists in root, if not check if file exists in ~webroot, if not throw real HTTP 404 error?

I do also have all my ErrorDocument's defined properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佞臣 2024-11-18 04:12:16

我知道这是不久前的事,但想插话一下。对于命中 404 的重写规则,我总是使用这个:

RewriteRule ^(.*)$ - [R=404,L]

我不确定它是否正确,但它对我有用。我很确定它应该始终带有“L”以防止进一步重写。

顺便说一句,“B”标志有什么用?我只知道 apache mod_rewrite 页面上列出的那些,所以我不知道它的含义。

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html编辑

:啊,我的问题是我查看旧版本的文档。谢谢你,旋转游戏。如果您没有指出这一点(需要更新我的书签),我可能永远不会知道我正在查看旧文档。

http://httpd.apache.org/docs/2.3/rewrite/flags .html#flag_b

I know this was a while ago, but wanted to chime in. For a rewrite rule to hit a 404, I always use this:

RewriteRule ^(.*)$ - [R=404,L]

I'm not sure if it's correct, but it works for me. I'm pretty sure it should always be with "L" to prevent further rewriting.

BTW, what is the "B" flag for? I only know the ones listed on the apache mod_rewrite page, so I'm lost on it's meaning.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

EDIT: ah, my problem is I look at older version docs. Thank you, Swivelgames. I probably would have never even known I was looking at the older docs if you hadn't pointed that out (need to update my bookmarks).

http://httpd.apache.org/docs/2.3/rewrite/flags.html#flag_b

听风念你 2024-11-18 04:12:16

如果您可以将 404 页面设为 PHP 文件,则可以在任何输出之前添加此文件以获得真正的 404:

<?php header("HTTP/1.1 404 Not Found"); ?>

这将导致 PHP 触发“真正的”404(从浏览器的角度来看),该 404 被传回浏览器。如果您的统计数据包位于服务器内部,则这种方法可能有效,也可能无效,我不确定。

我能想到的唯一其他解决方案是重定向到不存在的文件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/non-existent-404-generator
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /non-existent-404-generator [B,L]

If you can make your 404 page a PHP file, you can add this before any output to get real 404's:

<?php header("HTTP/1.1 404 Not Found"); ?>

This will cause PHP to trigger a 'real' 404 (from the browsers point of view) which gets passed back to the browser. If your statistics package is internal to your server this approach may or may not work, I'm not sure.

The only other solution I can think of is to redirect to a non-existent file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/non-existent-404-generator
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /non-existent-404-generator [B,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文