How to use .htaccess (in htdocs) to remove .php file extension from URL

Username and Website URL

I’m Trayeus

Error Message

I have read many posts and articles about how to use .htaccess as well as how to use it on InfinityFree. I just can’t seem to get it to work though.
I am trying to remove the .php extensions from my links but it won’t work. Here’s an example of some code snippets I have tried that didn’t work:

#Script for URL rewriting
#The line below may exist.
RewriteEngine On
#Rewrite all the rules, with / as the base, not requesting any other file or folder.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z]+)\/?$ $1.php [NC]


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
1 Like

This code is tested by me and I’m currently using this on my 2 websites which is hosted on Infinityfree.


RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

Can please explain how it doesn’t work? The code you’ve used works fine, but doesn’t redirect to, Is that what you mean?

This worked perfectly, thanks so much!

1 Like

Yes, correct. I couldn’t remove the file extension from the URL, but the code that @Amaanwarsi mentioned worked great!

1 Like

Glad to help you.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.