Remove .php with htaccess

Jun 9, 2014 00:00 · 165 words · 1 minute read Apache PHP

Do you ever get tired of your small website having .php after all the page names? Do you need to remove the .php from sub-directories as well as pages at the root level? Well then, I have the perfect snippet for you. I found this excellent little piece when I was also in need of some url clean up. I had to try quite a few solutions though before I found one that worked so I figured I would share this one for anyone else who needs it. All you need to do is place this at the base directory of your website inside a .htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

#hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Now you are the proud owner of clean urls! Happy Days!