0

Hi everyone this is my .htaccess file code

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

it works perfectly but i was wondering if can i force hide the .php extension, so even if i went to www.example.com/foo.php i get redirected to www.example.com/foo

is there any way i can do that ??

Benjie Wheeler
  • 555
  • 3
  • 16

2 Answers2

1

It is possible, but we must be careful not to generate a redirect loop, When non-php URL redirects to a php one. And php URL redirects to a non-php one. Plus I don't like this idea, because it will create an extra redirect, that will slow down the site response.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

#redirect .php to non-php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.php$ $1 [R=301,L]
user4035
  • 22,508
  • 11
  • 59
  • 94
0

You can match against the request instead of the URI to prevent looping:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]

You can append that to the end of you htaccess file.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220