0

I want to use a custom error page for ALL errors, so that there is no possible way that a visitor will see the servers built in error pages. But I seem to have found a glitch that I can not overcome.

The problem is that should a visitor mistype a URL with only spaces between two slashes, my environment seems to completely ignore my root folder htaccess file and sends the user to the built in error page.

The development environment is XAMPP on Windows 10. with the latest PHP & Apache etc.

To simplify the problem to it's root I am now using this HTACCESS File.

ErrorDocument 404  /statuspage.php
RewriteEngine On
RewriteRule ^ http://example.com [R=302,L]

The above code should redirect any and all URLs to example dot com. If that fails on not found pages, it should at least redirect to my error page. (which would probably then be caught and sent to example.com anyway.)

But when I type any of the following URLs, my entire project is ignored including the HTACCESS file and XAMPP throws out its own 404 error page.

1) www.mysite.com/ /

2) www.mysite.com/ /file.php

3) www.mysite.com/dir/%20/subdir/file.html

Apache replaces the space with %20. Ignores my redirect. And displays its own 404 page.

I should note that THESE errors are redirected just fine.

1) www.mysite.com/ file.php/ 2) www.mysite.com/dir/ x/file.html

I realize this error would not be common. But it can happen and I'm struggling to understand what is happening and how to stop it.

Gregor Macgregor
  • 495
  • 6
  • 12
  • 1
    IMHO you could move the logic to your URL router rather than apache. this will allow you to deploy the code on other platforms too. – Laur Ivan Jul 09 '16 at 18:28
  • I probably failed to explain how ignorant I am of such things. I have no idea what you mean by URL router. Are you talking about the httpd.conf or some other config file? If so my concern is that I may not be able to access that file in some hosting environments. If that is not what you meant, I would love to understand it better. Thanks. – Gregor Macgregor Jul 09 '16 at 18:34
  • 1
    Hmm. You leave the apache configuration as is and you treat the case in PHP. Normally, you have a file where paths are accounted for. Something like: *if path is "/something" then call function_something()*. [here's](http://stackoverflow.com/questions/125677/php-application-url-routing) some info to get you started. – Laur Ivan Jul 13 '16 at 07:49
  • Thanks @LaurIvan . I understand what you meant now. Actually that's exactly the problem. This is part of a little script I'm using to learn bootstrapping. Htaccess sends almost everything to the main index page which includes an app.php page with parcing , routing, etc... The problem is that when the URL contains "/ /" (slash space slash) or /%20/ Apache (or something) bypasses my script all together and throws up it's own error page. My URL routing is never even included. – Gregor Macgregor Jul 17 '16 at 12:22

2 Answers2

0

This is because RewriteRule directive runs before the ErrorDocument . Your rule redirects all requests (including 404) to http://example.com . To resolve this , you need to use a RewriteRule instead of the ErrorDocument to redirect not found requests to /statuspage.php , Replace your ErrorDocument with this :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /statuspage.php [L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thanks for the reply but I tried it and no good. www.mysite.com/ / still directs to the built in 404 document. Like you said, It SHOULD redirect any incorrect urls to /statuspage.php but it fails if the URL contains "/ /". In my example, I was using RewriteRule ^ http://example.com [R=302,L] as a test. It should redirect all request, good or bad, but it fails if the URL contain slash space slash. Your answer should work. But it doesn't. – Gregor Macgregor Jul 10 '16 at 11:49
0

After some research this seems to be a problem specific to my development environment.

My error log shows the error ID AH00127 When I Googled AH00127 I found that every single post involved a specific setup.

XAMPP for windows using a virtual host in the configs. I.E.

 <Virtualhost *:80>

Since I will never use that on a live sight, the issue can be ignored.

Thanks to the responders.

Gregor Macgregor
  • 495
  • 6
  • 12