Custom 404 error issues with Apache - the ErrorDocument is 404 as well - apache

I am trying to create a custom 404 error for my website. I am testing this out using XAMPP on Windows.
My directory structure is as follows:
error\404page.html
index.php
.htaccess
The content of my .htaccess file is:
ErrorDocument 404 error\404page.html
This produces the following result:
However this is not working - is it something to do with the way the slashes are or how I should be referencing the error document?
site site documents reside in a in a sub folder of the web root if that makes any difference to how I should reference?
When I change the file to be
ErrorDocument 404 /error/404page.html
I receive the following error message which isn't what is inside the html file I have linked - but it is different to what is listed above:

The ErrorDocument directive, when supplied a local URL path, expects the path to be fully qualified from the DocumentRoot. In your case, this means that the actual path to the ErrorDocument is
ErrorDocument 404 /JinPortfolio/error/404page.html
When you corrected it in your second try, the reason you see that page instead is because http://localhost/error/404page.html doesn't exist, hence the bit about there being a 404 error in locating the error handling document.

.htaccess files are disabled by default in Apache these days, due to performance issues. When .htaccess files are enabled, the web server must check for it on every hit in every subdirectory from where it resides.
Just figured it was important to note. If you want to turn on .htaccess files anyway, here's a link that explains it:
http://www.tildemark.com/enable-htaccess-on-apache/

Instead of adding them to your .htaccess file, you can add them to any of your virtual host files.
<VirtualHost *:80>
DocumentRoot /var/www/mywebsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorDocument 404 /error-pages/404.html
ErrorDocument 500 /error-pages/500.html
ErrorDocument 503 /error-pages/503.html
ErrorDocument 504 /error-pages/504.html
</VirtualHost>
Where error-pages is a subfolder in the mywebsite folder, containing the custom error pages. Make sure you restart your apache to view your changes.
$sudo /etc/init.d/apache2 reload

Whatever url you enter as the default 404 page must be either absolute or relative from the root folder : That's why some make the mistake of treating its url like the rewrite engines' url which is relative from the folder where .htaccess is located.

Related

ErrorDocument redirect in .htaccess when placed in subfolder

My development server (localhost) is setup so that each website is in it's own sub-folder, like so:
localhost
\Site1
\Site2
Each sub-folder (Site1, Site2 etc) has it's own .htaccess:
ErrorDocument 404 /alert.php?error=404
ErrorDocument 403 /alert.php?error=403
ErrorDocument 401 /alert.php?error=401
This works absolutely fine when on the live server (as it's then located in the website root location), however, in my localhost development environment, I have to update the htacess to:
ErrorDocument 404 /site1/alert.php?error=404
in order to make it work. This means a lot of updating the .htaccess back and forth when I want to test locally vs uploading for the live version.
Is it possible for the .htaccess to determine the correct path to use based on the host enovirnoment?
You could calculate the "base directory" (the directory in which the .htaccess file is located relative to the document root) using mod_rewrite and use this in the ErrorDocument directive with the help of an Apache expression (requires Apache 2.4.13).
For example:
# Calculate the BASE_DIR based on the location of the .htaccess file (rel to DocumentRoot)
# For use in the ErrorDocument directive the BASE_DIR does not include the slash prefix
# eg. If in the root directory then BASE_DIR = "" (empty)
# If in /site1/.htaccess then BASE_DIR = "site1/"
RewriteCond %{REQUEST_URI}#$1 ^/(.*?)([^#]*)#\2
RewriteRule (.*) - [E=BASE_DIR:%1]
# Set ErrorDocument dynamically relative to BASE_DIR (Requires Apache 2.4.13+)
# The 2nd argument to the ErrorDocument directive must be prefixed with a slash
# in the source code itself for it to be interpreted as a local path.
ErrorDocument 404 /%{reqenv:BASE_DIR}alert.php?error=404
So, if the .htaccess file is located in the document root then the ErrorDocument is set to /alert.php?error=404 and if in the /site1 subdirectory then it's set to /site1/alert.php?error=404.
Aside: When using PHP you don't need to explicitly pass the error code to the alert.php script, since this is available in the $_SERVER['REDIRECT_STATUS'] superglobal. This also allows you to determine if the alert.php script was called directly.
HOWEVER, it would be preferable to define each site in it's own <VirtualHost>, with it's own DocumentRoot, rather than using subdirectories, and have exactly the same directory/URL structure for local development as on the live server - no messing. There are bound to be other places that are affected by the different path-depth, such as client-side links to static resources?

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

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:
A local URL to redirect to (if the action begins with a "/").
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.
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?
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.

Exclude a directory from custom ErrorDocuments

Currently I have a few ErrorDocuments in my .htaccess (which is located in the www/site directory) like ErrorDocument 404 /site/error, which work.
However, there is one directory in this site dir which should not have these custom error documents. So whenever an user tries to access /site/no_custom_errors/non_existing_file.png, I should get the default Apache error page and not my custom error.
How would I achieve this?
Your apache config for that directory can change it, or if "AllowOverride FileInfo" (see: http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride) is active for that site or directory, then an .htaccess file in that directory will work containing:
ErrorDocument 404 /site/otherErrorDoc
see: http://httpd.apache.org/docs/current/mod/core.html#errordocument
Alternatively, it could be done in a <directory> directive in httpd.conf or in a virtual host like so:
<Directory path/to/special/directory>
ErrorDocument 404 /site/otherErrorDoc
</Directory>
see: http://httpd.apache.org/docs/2.2/mod/core.html#directory

.htaccess ErrorDocument issue

Problem with custom error pages.
My server root is "C:\www" and error pages are in "C:\www\errors".
So I put a .htaccess file in "C:\www" with following:
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
This works for anything in "mysite.net". But I also have subdirectories like "first.mysite.net", "second.mysite.net" and so on, and .htaccess from root doest work there. It's searching for "errors" folder in these subfolders and return the default 404 page.
I've tried this:
ErrorDocument 500 C:/www/errors/500.html
ErrorDocument 500 C:\www\errors\500.html
Doesn't work.
ErrorDocument 500 http://mysite.net/errors/500.html
This works, but it makes a redirect and I don't want this. I want the browser to keep adress line the same.
Can anyone make this clear for me? Thanks.
If they are subdomains with seperate virtualhost definitions you need to check virtualhost definitions and AllowOverride settings for those subdomains. See http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride and change AllowOverride setting as needed.

Where to save your .htaccess file on Apache for 404 page

I have an Apache web server running on Amazon EC2 and I am trying to make a custom 404 page which I did, but I want it to popup when a mistyped or wrong URL is requested. This is my .htaccess code
ErrorDocument 404 /404.html
I saved my .htaccess file in my /var/www/html directory. Am I doing something wrong? Because when I typed in a wrong url, the default Apache 404 Error keeps popping up and I don't want that to drive visitors away. Is this possible? Thanks!!!
confirm that AllowOverride directive is set to All in your httpd.conf
secondly .htaccess should be kept inside the root directory of your code.
I figured it out. I used the same code:
ErrorDocument 404 /404.htmlBut instead of putting it in my .htaccess file, which never worked, I put the same code in my httpd.conf file which I found under the directory /etc/httpd/conf/httpd.conf Thanks for all your help!