local URL for apache ErrorDocument directive not working in .htaccess file - 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 :
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.

Related

How to show custom 404 error page when using `AllowEncodedSlashes On`

I recently came across the AllowEncodedSlashes Apache directive:
https://intranet.csc.liv.ac.uk/manual/mod/core.html#AllowEncodedSlashes
If I hit a URL like this: www.domain.com/my/page/%2F/etcetera, Apache shows its default 404 error page:
However, I would like to return a custom 404 error page but how can I do it within my .htaccess? This doesn't work:
ErrorDocument 404 /entrypoint.php
I know I can do it if I have access to the server's httpd.conf configuration file (I tested locally). But what if I do not have access to the configuration on a remote site? Is there a way?
Thank you for the attention.

Apache not processing encoded URLs with %3F

The problem url links to my website are of the form
/fullpage.php%3F%20cp3_Hex%3D0F0200%26cp2_Hex%3D000000%26cp1_Hex%3DFC2024
The un-encoded url is
/fullpadge.php?cp3_Hex=0F0200&cp2_Hex=000000&cp1_Hex=FC2024
Apache returns a:
403: You don't have permission to access /fullpage.php? cp3_Hex=0F0200&cp2_Hex=000000
I have tried the following rewrite rule
RewriteRule ^/fullpage.php%3F(.*)$ /fullpage.php?$1
to no avail
Any ideas
You are almost certainly getting a 403 error.
The error is caused because ? is a banned file/directory name character on Windows and Linux. This means when Apache attempts to find a file or directory named "/document/root/index.php?blah" (after decoding) and it causes a 403 error. This is before the .htaccess files are read so you cannot use mod_rewrite in the .htaccess file to override this 403 error or an ErrorDocument defined in the .htaccess file to catch this error.
The only way to catch %3f is to use mod_rewrite or an ErrorDocument in a "VirtualHost" e.g. in httpd-vhosts.conf (or the main server configuration if there aren't any "Virtualhost"s e.g. in httpd.conf).

notfound.html is not found

Pretty sure based on various examples (like the one here) I set things up correctly:
1) I created a file 'notfound.html' in the root directory of my site
2) I'm running xampp, ie. Apache web server, so based on the stuff I've read, Apache looks for a notfound.html in the root directory of a site when a non-existent page is typed in the browser, such as www.mysite.com/NotAValidPage.html and uses that notfound.html for the 404 error message
Here's what I've tried and the result of each attempt:
1test) I put my own notfound.html in my site's root directory and enter a non-existent page
1result) my notfound.html is not used, but instead the default 404 error from (I guess) my web server, appears and says:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try
again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
2/14/2012 9:11:29 AM
Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
2test) I next added an .htaccess file to the root folder of my site and added this line in this .htaccess file:
ErrorDocument 404 /notfound.html
then I typed a non-existent page into my browser.
2result) I still see a default 404 error page, not my notfound.html, only this time the default error page says:
Not Found
The requested URL /mysite.com/indsadfaex.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying
to use an ErrorDocument to handle the request.
3test) I then modified my .htaccess file by removing the '/' before the name of my custom 404 error page filename as follows:
ErrorDocument 404 notfound.html
3result) Now when I type my site name followed by a non-existent page, I no longer see the default 404 error page -- the browser window is completely blank except for the following at the top left corner:
notfound.html
Not sure what to do here, seems like I am missing some subtlety -- any ideas are really appreciated.
EDIT: I doubt it matters but there are 2 other statements in my .htaccess file for redirection situations:
RewriteEngine on
RewriteRule ^([^/\.]+)$ theHandlerPage.php?theArgumentPassedIn=$1 [L]
The regex here simply says 'go to the very end of the URL typed in and match a contiguous string of characters iff no '/' or '.' is in that contiguous stream of characters, then pass those in by way of $_GET[theArgumentPassedIn'] to the file theHandlerPage.php.
I don't think this rewrite rule is coming into play here because it will not match on a bad page name such as mysite/badpage.html because of the '.' in badpage.html.
Okay -- I discovered a bit of unexpectedness.
1) I move my notfound.html error file to the parent directory of my site -- whereas before the file was located at c:/xampp/htdocs/mysite/notfound.html, I have now moved this file to c:/xampp/htdocs/notfound.html.
2) then I use ErrorDocument 404 /notfound.html inside of my .htaccess file, which is located at c:/xampp/htdocs/mysite/.htaccess
And now when I type in the browser bar mysite/aBadfilename.html, my 404 notfound.html is showing up fine.
BUT THIS IS UNWORKABLE. I cannot have my notfound.html reside in the parent directory of my site, because I need a different notfound.html for each site I develop. I must be able to put my site's notfound.html in its folder space, ie. inside c:/xampp/htdocs/mysite.
Because c:/xampp/htdocs is the root folder of all web sites on the server.
Why is my web server looking for my-site's notfound.html in the c:/xampp/htdocs root directory instead of c:/xampp/htdocs/mysite? After all, I type mysite/badfile.html so the web server (in my opinion) should understand that notfound.html is in the c:/xampp/htdocs/mysite folder space.
Are you using Virtual Hosts ?
if so, i think there is a problem, open your httpd-vhosts.conf and look for a line where is DocumentRoot C:/xampp/htdocs/bla/blah/mysite/ and write down full path to a host, if it doesn't work play with Directory path. Hope it helps.

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

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.

Apache ErrorDocument with absolute path

I have a server with several virtual hosts. Now I want to set up the error documents for the whole server. I have located my error sites in /var/www/error/*, but with the ErrorDocument directive I am only able to set the error document relative to the document root, but I want to use the absolute path (e.g /var/www/error/404.html).
Has anyone an idea how I can get this?
I don't think this can be done directly inside the statement: The ErrorDocument will always have to be relative to the DocumentRoot. According to the docs, the only alternative seems to be specifying an external URL, but that is bad because the wrong response header gets sent (302 instead of 404).
You could try whether anything is possible using an Alias directive or - I'm sure this would work - a symbolic link:
ErrorDocument 404 /symlinked_page.php