Reputation: 1
What I’m trying to do is use .htaccess mod_rewrite to remove the .php extension from the url so if someone types a page with the php extension such as about.php, a 404 error will be shown and if someone clicks on any of the links in the navigation menu or any link located within the site on any page, the url will default to the file name without the .php extension. How do I get this to work?
Upvotes: 0
Views: 101
Reputation: 784898
You may use these rules in your site root .htaccess:
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
## To internally rewrite /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Upvotes: 1