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?
1 Answer
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]
4 Comments
Alvin
The error is Forbidden You don't have permission to access this resource. Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
Alvin
The .htaccess has exactly what you posted above. There's nothing else in it.
anubhava
ok then I will have to stop at this point. These rules are working for each and every case from my .htacces in site root. There might be some local issues on your site that I can guess.
Alvin
Ok. Thank you for trying to help me.