使用.htaccess来设置位于同一文件夹中的404错误文档 [英] Use .htaccess to set 404 error document located in the same folder

查看:111
本文介绍了使用.htaccess来设置位于同一文件夹中的404错误文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我试图将错误文件的路径设置为与.htaccess位于同一目录(位于网站根中)相同,如下所示:

So... i am trying to set the path to an error document located in the same directory as the .htaccess (located in the WEBSITE "root") like this:

       ErrorDocument 404 404.php

但是不是显示网页...它只是在屏幕上写404.php。我不能使用绝对路径,例如:

However instead of showing the webpage...it just writes 404.php on the screen. I can't use absolute paths like:

       ErrorDocument 404 /404.php

因为我没有为我做网站,并且我不知道网站将存储在该文件夹的绝对路径...

Because i am not doing the website for me and i do not know the absolute path of the folder the website will be stored on...

我的问题是:我可以在.htaccess文档中设置相对路径,而不是绝对路径吗? (如果可以,怎么办?)

My question is: Can i set relative paths in the .htaccess document rather than absolute ones? (and if yes...how?)

推荐答案

ErrorDocument指令的参数中的URL路径始终相对于DocumentRoot。

The URL-path in the argument of the ErrorDocument directive is always relative to the DocumentRoot.

但是,您可以使用mod_rewrite模块来解决此问题。

However, you can work around this using the mod_rewrite module.

RewriteEngine on
#if requested resource isn't a file
# and isn't a directory
# then serve local error script 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .* 404.php [L] 

确保404.php确实发出404响应标头!

Make sure the 404.php does actually emit a 404 response header!

我还没有测试过,但这应该可以帮助您入门。另外请注意,始终最好使用ErrorDocument指令,并且询问相对于DocumentRoot的站点将位于何处是明智的,因此您可以编写

I haven't tested it, but this should get you started. Also note that using the ErrorDocument directive is always preferable to this, and asking where the site will reside relative to DocumentRoot would be sensible, so you'd be able to write

ErrorDocument 404 /path/to/my/site/404.php

这篇关于使用.htaccess来设置位于同一文件夹中的404错误文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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