local URL for apache ErrorDocument directive not working in .htaccess file

本秂侑毒 提交于 2019-12-12 06:45:27

问题


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 :

The syntax of the ErrorDocument directive is:

ErrorDocument <3-digit-code> <action>

where the action will be treated as:

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

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

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

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

index.php: main page

404.php: a custom 404 page

.htaccess:

RewriteEngine On 

ErrorDocument 404 /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?


回答1:


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.

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

I suggest you create a fake domain in your hosts file and set up a vhost with it.

Hosts on OSX :

sudo nano /etc/hosts

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

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

and enter something like this:

127.0.0.1 myfakedomain.com

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

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.

Then you will be able to run your site using

myfakedomain.com/ 

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



来源:https://stackoverflow.com/questions/45356314/local-url-for-apache-errordocument-directive-not-working-in-htaccess-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!