.htaccess 和针对不同文件类型的自定义 ErrorDocument

发布于 2024-08-19 16:12:56 字数 282 浏览 4 评论 0原文

我想知道是否可以使用 mod 重写和 ErrorDocument 减速来根据请求的文件类型来自定义错误页面。

例如,请求一个不存在的 html 或 php 文件,Apache 将提供漂亮的自定义 HTML 页面。

但是,如果请求一个不存在的图像、js、css 等文件,那么 Apache 将提供一个基本的 html 文件,其中只有一个链接。

Facebook 就是这种行为的一个很好的例子。尝试请求一个伪造的 JavaScript 文件,如果您请求一个不存在的 php 文件,您将收到一个不同的页面。

I'm wondering if it's possible to use mod rewrite along with the ErrorDocument deceleration to customize the error pages depending on what type of file is requested.

For example a non-existent html or php file is requested Apache will give nice custom HTML page.

But if a non-existent image, js, css, etc... file is requested then Apache will serve out a basic html file with only a link on it.

A good example of this behavior is Facebook. Try requesting a bogus JavaScript file, you will receive a different page then if you were to request a non-existent php file.

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

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

发布评论

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

评论(3

装迷糊 2024-08-26 16:12:56

最终使用 ErrorDocument 和 RewriteRules 的组合,这有效,因为 php 页面对我抛出 404 Not Found 。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*(?<!.js|.css|.ico|.txt|.bmp|.gif|.png|.jpeg|.jpg)$ /error/404.php [L]

ErrorDocument 404 /404_basic.html

Ended up using a combination of both ErrorDocument and RewriteRules this works because the php page throws a 404 Not Found for me.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*(?<!.js|.css|.ico|.txt|.bmp|.gif|.png|.jpeg|.jpg)$ /error/404.php [L]

ErrorDocument 404 /404_basic.html
〆一缕阳光ご 2024-08-26 16:12:56

使用 RewriteCond !-f 以及对所需输出页面的重写和 R=404 标志。

Use RewriteCond !-f along with a rewrite to the desired output page and a flag of R=404.

北笙凉宸 2024-08-26 16:12:56

您可以为您的 404 页面使用 PHP 脚本:

ErrorDocument 404 /error404.php

您可以在其中使用 PHP 分析 URL 路径(请参阅 $_SERVER['REQUEST_URI'])并确定已请求或期望的资源类型。

You could use a PHP script for your 404 page:

ErrorDocument 404 /error404.php

There you can analyze the URL path with PHP (see $_SERVER['REQUEST_URI']) and determine what kind of resource has been requested or is expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文