Sometimes it just doesn’t look nice for the end user when they visit a website and URL has the .php at the end of it. For example, if you have a page accessible at www.mysite.com/example.php – for aesthetic purposes, you might want that URL to be www.mysite.com/example instead. The code below will show you how to achieve this. Note that the code should go in the .htaccess file in your root folder.
The Code
1 2 3 4 5 6 7 8 |
# 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] |