jameson
jameson

Reputation: 63

.htaccess redirect all extension to php

I recently updated my .htaccess file to hide the .php extension of the urls. It works nice, when I try to reach domain.com/index, it displays the php file.

When I type domain.com/index.php it redirects me to the domain.com/index url and displays the .php file as well.

I would like to redirect all other extensions to the no extension url to display the .php file for security reasons. My problem is, that I can not configure my .htaccess to redirect well known extensions like .html .asp. aspx .shtml etc. to the non-extension url, and get the .php file displayed as the content.

My .htaccess file looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]


# no extension to the .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

# this is for the index
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Upvotes: 1

Views: 1834

Answers (1)

Arthur
Arthur

Reputation: 3807

You could try something like this:

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.[a-zA-Z0-9]+$ $1 [R=301,L]

Then it will not only redirect .php extension, but anyone. To use it, be sure that you dont have URL with a dot and an alphanumeric string at the end.

Upvotes: 2

Related Questions