0

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
  • 1
    How far have you gotten with your research? Could you share examples of your attempts? Commented Apr 28, 2020 at 13:40

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

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.
The .htaccess has exactly what you posted above. There's nothing else in it.
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.
Ok. Thank you for trying to help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.