设置Web服务器处理404错误对于某些文件类型使用的.htaccess(ErrorDocument的404 +重写规则) [英] Set web server handle 404 errors for some file types using .htaccess (ErrorDocument 404 + rewrite rules)

查看:210
本文介绍了设置Web服务器处理404错误对于某些文件类型使用的.htaccess(ErrorDocument的404 +重写规则)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在这个伟大的社区我的第一个问题(请告诉我,如果我做错什么)。我学到了很多东西从你的回答在过去5 - 6年,现在我有非常具体的问题,我不能在互联网上找到(这可能吗?)...

我们的目标是要设置自定义的ErrorDocument 404,处理不存在的选择静态文件的,如: /notfound.jpg 的和一个子目录(任何目录): /dir/notfound.jpg 的,根据字preSS与pretty的永久链接上运行的网站启用。这将避免高负载的服务器上的某些选择的静态文件丢失或连接不正确时!

我用我的.htaccess文件从W3的总缓存插件略加修改code:

 的ErrorDocument 404 /_$c$​​c_404.html

#BEGIN(修改)W3TC跳过404错误处理的Word preSS对静态文件
< IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteCond%{REQUEST_URI}!(机器人\ .TXT |网站导航\的.xml(\ GZ)。?)
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME} \(CSS | JS | BMP | GIF | ICO | JPG | JPEG | JPE | PNG | SWF)。$ [NC]
重写规则。*  -  [L]
< / IfModule>
#END(修改)W3TC跳过的Word preSS对静态文件404错误处理

#BEGIN字preSS
< IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /
重写规则^指数\ .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
< / IfModule>
#结束字preSS
 

我上交友2不同的服务器尝试它,共享计划,既让Word preSS与pretty的永久链接,结果是一样的:

  1. 作品就像预期的一样请求的 http://mydomain.com/notfound.jpg

  2. 有关文件未找到在这样的子目录不工作的 HTTP:/ /mydomain.com/dir/notfound.jpg 的。在这种情况下,WP处理请求并显示404页,这是我想避免的某些文件类型,即像静态文件:CSS,JS,JPG和一些更多...

任何答复和意见是值得欢迎的,谢谢!我可以尝试任何建议,你可能有(甚至是未经测试)。

修改1:的说明对方设法得到它的工作(见接受的答案+下面的注释):

看来你必须(或取决于服务器配置)设置自定义404错误页面,否则将改写这排除了一些选择的文件类型将无法工作,因此在WP将处理该请求! (似乎没有多少人注意到这一点,或者它可能与服务器配置)

我意识到这一点,因为我想提出的解决方案之前我写了这个问题,但没有的ErrorDocument 404行并没有工作。

编辑2:测试和另注:

  1. 在测试期间我测试了查询字符串的URL:/dir4/12345.css?v=3&t=66 [它工作得很好,因为REQUEST_URI不包括查询字符串/12345.css?v=5和!]

  2. 要注意的很重要的一点是,服务器通常应返回HTTP code 404丢失的文件(否则搜索引擎会伤害你的排名)。 ErrorDocument的404 HTTP:如果您在设置完整的域名//mydomain.com/_$c$​​c_404.html,那么在客户端将不会收到原始的错误状态code,反而会收到一个重定向状态code。 (参考 - httpd.apache.org/docs/2.0/mod/core.html#errordocument)

解决方案

您可以试试这个:

 的ErrorDocument 404 /_$c$​​c_404.html

< IfModule mod_rewrite.c>
RewriteEngine叙述上

的RewriteCond%{REQUEST_URI}!(机器人\ .TXT |网站导航\的.xml(\ GZ)。?)
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI} \。(CSS | JS | BMP | GIF | ICO |?JPE G | JPE | PNG | SWF)$ [NC]
重写规则。*  -  [L]

重写规则^指数\ .PHP $  -  [L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]

< / IfModule>
 

It's my first question in this great community (please tell me if I'm doing anything wrong). I learned a lot of from your answers in the past 5-6 years and now I have very specific question, which I couldn't find on the Internet (is this possible?)...

The goal is to set custom ErrorDocument 404, that handles not existing chosen static files like: /notfound.jpg and in a subdirectory (for any directory): /dir/notfound.jpg, for sites running under Wordpress with pretty permalinks enabled. It will avoid high load on the server when some chosen static files are missing or linked incorrectly!

I'm using a slightly modified code from the W3 Total cache plugin in my .htaccess file:

ErrorDocument 404 /_code_404.html

# BEGIN (modified) W3TC Skip 404 error handling by WordPress for static files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(css|js|bmp|gif|ico|jpg|jpeg|jpe|png|swf)$ [NC]
RewriteRule .* - [L]
</IfModule>
# END (modified) W3TC Skip 404 error handling by WordPress for static files

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I'm trying it on 2 different servers with Cpanel, on shared plans, both have Wordpress with pretty permalinks and the result is the same:

  1. Works just as expected for requests like http://mydomain.com/notfound.jpg

  2. Doesn't work for not found file in a subdirectory like this http://mydomain.com/dir/notfound.jpg. In this case the WP handles the request and shows 404 page, which I want to avoid for certain file types, i.e. for static files like: css, js, jpg and a few more...

Any answers and comments are welcome, thanks! I can try any suggestions you may have (even untested).

EDIT 1: A note to each other trying to get it to work (see accepted answer + the note below):

It seems you must (or depend on server config) set custom error 404 page, otherwise the rewrite which excludes some chosen file types will not work and as a result the WP will process the request! (it seems not much people note this, or it could be related to the server config)

I realized that, because I was trying the proposed solution even before I write this question, but without the "ErrorDocument 404" line and it didn't work.

EDIT 2: Tests and another note:

  1. During the tests I tested the URLs with query string: /12345.css?v=5 and /dir4/12345.css?v=3&t=66 [It works fine, because REQUEST_URI excludes query string!]

  2. Very important thing to note is that the server usually should return HTTP code 404 for missing files (otherwise search engines can hurt your rankings). If you set the full domain name in: ErrorDocument 404 http: //mydomain.com/_code_404.html, then the the "client will not receive the original error status code, but instead will receive a redirect status code". (reference - httpd.apache.org/docs/2.0/mod/core.html#errordocument)

解决方案

Can you try this:

ErrorDocument 404 /_code_404.html

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.(css|js|bmp|gif|ico|jpe?g|jpe|png|swf)$ [NC]
RewriteRule .* - [L]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

这篇关于设置Web服务器处理404错误对于某些文件类型使用的.htaccess(ErrorDocument的404 +重写规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆