11

I have the following rewrite in my .htaccess file which removes the .php extension from files, converting for example so.com/question.php to so.com/question.

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

However this also breaks the default DirectoryIndex behaviour, in which just typing the directory will redirect to the index file in the folder, e.g. so.com/answer displays so.com/answer/index.php

Simply combining the above code with DirectoryIndex index.php does not achieve both results.

Can someone help me combine these two functions, or rewrite the code to exclude index.php files, which would achieve the same result?

ajcw
  • 23,604
  • 6
  • 30
  • 47

3 Answers3

25

I'm thinking you just need to verify that the file exists prior to doing the rewrite, that way you'll leave 404 and directoryindex behaviours intact:

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

(not tested)

Timothée Groleau
  • 1,940
  • 13
  • 16
  • @Timothée Groleau nice code, is there any option to remove 'index.php' for home page ? – Mo. Apr 02 '13 at 03:47
  • working fine, i had little complex case, http://example.com/page.php?id=3479&name=this-is-page-name i had to convert it to http://example.com/page/?id=3479/name=this-is-page-name – Dheeraj Thedijje Aug 28 '15 at 09:34
2
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

I tested and it is working fine.

kiran malvi
  • 1,058
  • 10
  • 21
1
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

verify files and folder and also, add RewriteBase /

Crsr
  • 624
  • 3
  • 9