Home .htaccess ErrorDocument unexpected behavior
Reply: 1

.htaccess ErrorDocument unexpected behavior

T.van.Hesteren
1#
T.van.Hesteren Published in 2018-02-07 21:50:22Z

I use the following rules in my .htaccess to redirect both 404 and 500 errors:

ErrorDocument 404 /404/
ErrorDocument 500 /500/

However, it doesn't always redirect properly. E.g:

https://example.com/Test --> 404 REDIRECT OK
https://example.com/Test/Test --> 404 REDIRECT OK
https://example.com/test.php --> 404 REDIRECT FAILED (It fails as soon as it has the .php extension?).

As shown above, when the url path has a .php extension, the 404 redirect fails with the error Primary script unknown as shown below:

[Wed Feb 07 22:44:36.823117 2018] [proxy_fcgi:error] [pid 24747:tid 140368612202240] [client xx.xxx.xxx.xxx:34028] AH01071: Got error 'Primary script unknown'

What am I doing wrong and why is the redirect working properly without the .php extension and fails when I add the .php extension to the url path? The tested paths are verified to be non-existing.

Nikita Agrawal
2#
Nikita Agrawal Reply to 2018-02-08 04:23:25Z

Try this in your .htaccess

ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]

RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]

# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354813 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO