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?)
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
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
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With