Tygo
Tygo

Reputation: 192

.htaccess rewrite ErrorDocument not being redirected

I removed the php directory from my URL and remove the .php extension. But now I am trying to add my 404/500 error to make my website look nice.

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /php/$1 [QSA,L]

ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

When I redirect the errorDocument to an external page it works but not to my local file. I get a 500 Internal Server error. I could make a workaround and just make it go to an external page what will be my page. But that's not really clean. Is anyone able to help me with this?

Upvotes: 1

Views: 73

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

I think you must match against DOCUMENT_ROOT as well as add this RewriteCond %{REQUEST_FILENAME}\.php -f before adding .php like this :

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI}\.php -f
# or you could do it like this 
# RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI} -f
# without php according to your setting
RewriteRule ^(.*)$ /php/$1 [QSA,L]


ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

because whenever you send wrong request to any target destination you must make sure that target will handle it and there are enough conditions to classify which one should go , otherwise server will generate error.

Upvotes: 1

Related Questions