apache ErrorDocument指令的本地URL在.htaccess文件中不起作用 [英] local URL for apache ErrorDocument directive not working in .htaccess file

查看:104
本文介绍了apache ErrorDocument指令的本地URL在.htaccess文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用本地URL(相对路径)显示我的自定义404错误消息,但它似乎没有按预期运行.如apache文档所述,此处:

I want to use local URL (relative path) to show my custom 404 error message and it doesn't seem to be working as expected. As apache documentation stated here :

ErrorDocument指令的语法为:

The syntax of the ErrorDocument directive is:

ErrorDocument <3-digit-code> <action>

该操作将被视为:

  1. 要重定向到的本地URL(如果操作以"/"开头).

  1. A local URL to redirect to (if the action begins with a "/").

要重定向到的外部URL(如果操作是有效的URL).

An external URL to redirect to (if the action is a valid URL).

要显示的文本(如果以上都不显示).如果文本包含多个单词,则必须用引号()引起来.

Text to be displayed (if none of the above). The text must be wrapped in quotes (") if it consists of more than one word.

方法2和3正常工作.为了进行测试,请想象一个名为test的文件夹,其中包含3个文件.

Methods 2 and 3 are working correctly. For testing purpose imagine a folder named test with 3 files.

index.php:主页

404.php:自定义404页面

.htaccess:

RewriteEngine On 

ErrorDocument 404 /404.php

现在访问此URL http://localhost/test/blah-blah不会显示我的404.php页面,而是显示默认的未找到页面,并显示以下消息:

Now accessing this URL http://localhost/test/blah-blah does not show my 404.php page instead a default Not Found page is displayed with this message:

The requested URL /tests/test-htaccess/asdasd was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an 
ErrorDocument to handle the request.

有什么办法解决这个问题吗?

Any idea how to fix this?

推荐答案

自您提出问题以来,您可能已经找到了解决问题的方法,但是对于遇到同样问题的任何人,我要说的问题是使用本地主机运行您的站点.

You probably found the solution to your problem since the time you asked, but for anyone having the same kind of issue, I would say the problem is that you are running your site with localhost.

该消息说,Apache在尝试获取为Errordocument 404指定的文件时遇到404错误,因此看不到

The message says that Apache encountered a 404 error when trying to get the file specified for Errordocument 404, so it doesn't see

/404.php

我建议您在主机文件中创建一个虚假域,并使用它来建立虚拟主机.

OSX上的主机:

sudo nano /etc/hosts

Windows上的主机,右键单击此文件以管理员模式进行

Hosts on Windows, Right-click this file to edit in administrator mode :

 C:\WINDOWS\System32\Drivers\Etc\Hosts

并输入如下内容:

127.0.0.1 myfakedomain.com

要设置虚拟主机,您必须在httpd.conf中取消对对httpd-vhosts.conf的调用的注释(在此情况下,使用MAMP几乎会结束)

To set up virtual hosts, you must uncomment the call to httpd-vhosts.conf in httpd.conf (near the end, using MAMP in this case)

# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

但是此文件的位置取决于您正在使用的堆栈(Wamp,Mamp,Xampp等),因此请在其文档中使用关键字"Virtual host"进行搜索.

but the location of this file depends of the stack you are using (Wamp, Mamp, Xampp, etc) so search with the keyword "Virtual host" in its documentation.

然后,您将可以使用

myfakedomain.com/ 

在您的浏览器中,应正确处理404错误.

in your browser and 404 errors should be handled the right way.

这篇关于apache ErrorDocument指令的本地URL在.htaccess文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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