Use .htaccess to set 404 error document located in the same folder

北慕城南 提交于 2020-05-14 19:04:38

问题


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

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...

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


回答1:


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

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] 

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

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



回答2:


While a number of directives are sensitive to relative directory while within an .htaccess or <Directory> context, ErrorDocument is not. Per the documentation:

URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot), or be a full URL which the client can resolve.

Source




回答3:


ErrorDocument paths are relative to the DocumentRoot server setting, which is usually found in /etc/apache2/sites-enabled/000-default.conf This is how it looks in my file:

<VirtualHost *:80>   <---------------- VirtualHost on port 80 is generally internet traffic
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/   <-------- this is what we're looking at
</VirtualHost>

Note that the DocumentRoot set in this file is absolute in the filesystem - var is in the bottom level folder /.

So my .htaccess file resides in /var/www/myapp/.htaccess, meaning that the .htaccess file exists one directory down from DocumentRoot (which is set to /var/www/). So in order to have my ErrorDocument 404 served from /var/www/myapp/errordocs/404.html, I need to set, in my .htaccess, the following:

ErrorDocument 404 /myapp/customErrorPages/custom_404.html

Note that even though the .htaccess file resides in the myapp folder, I still need to include the /myapp/ in the path, as the path is relative to the DocumentRoot as defined in /etc/apache2/sites-enabled/000-default.conf

Also note that the path must start with a / in order for apache to read it as a path. Setting "myapp/customErrorPages/custom_404.html" (without the leading /) will result in just that string being printed instead of a document being served.

Pretty confusing if you don't have knowledge of directory paths in linux and configuration files in apache, but keep poking around you'll get it ;)



来源:https://stackoverflow.com/questions/20546861/use-htaccess-to-set-404-error-document-located-in-the-same-folder

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