-1

I am using the code below in my htaccess file that always sends the site to the www version and allows the file extension to be removed if php. However, if a visitor types the extension, it still shows it.

How do I remove the file extension even if the visitor types it?

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
SteveMills04
  • 119
  • 8
  • This question has been asked before: http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess. Try reading through the accepted answer and see if that works for you. – V13Axel Mar 06 '15 at 21:23
  • You need to redirect to the non-php version, like `^(.*?)(.php)?$` – eckes Mar 06 '15 at 21:26

1 Answers1

0

Try this:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^(.*)\.php$ [NC] 
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

If you want the redirection to non-php version to only happen when the php actually exists, use

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.*)\.php$ [NC] 
RewriteRule ^ %1 [R=301,L]
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89