Shafeeque M
Shafeeque M

Reputation: 1

Showing 500 Error instead 404 after .htaccess configuration

I've recently added following lines to the .htaccess file:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

What I'm trying to achieve: For PHP files:

example.com/abc.php should be redirected as example.com/abc
example.com/abc/ should be redirected as example.com/abc
example.com/abc should load as example.com/abc

For Directories, load with normal behavior: Note: xyz is a directory

example.com/xyz should load as example.com/xyz/
example.com/xyz/ should load as example.com/xyz/

The problem is if someone tries example.com/abc/bla, it throws 500 error instead of 404. Can someone help to modify the above .htaccess lines to fix the 500 error issue?

Upvotes: 0

Views: 62

Answers (1)

Shafeeque M
Shafeeque M

Reputation: 1

After many corrections, i got this issue resolved. Here is final code of htaccess

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
#Enforce a no-trailing-slash policy.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# 404 ANY URL WITH ADDITIONAL PATH INFO ##
RewriteCond %{PATH_INFO} .
RewriteRule ^ - [R=404]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

This code does the following

example.com/abc.php redirecting as example.com/abc
example.com/abc/ redirecting as example.com/abc
example.com/abc showing as example.com/abc
example.com/abc/nopage redirecting to 404

Upvotes: 0

Related Questions