Last Updated on February 6, 2022 by Amit

Remove .php using htaccess

We all know that a URL without file extension at the end looks clean and easy to remember.
Removing file extension so that the URL can look preety and clean is easy with mod-rewite An apache module that allows its users to rewite/Redirect  and manipulate website  URLs.  Mod-rewrite comes pre installed with Apache server so you do not need to do anything except modifying your server.config or htaccess. In this post we will learn to remove .php from website URLs. 

To get started, you must have an Apache server and access to either server.config or htaccess file. 

Remove .php from URLs

You can remove .php traling prefix from your URLs using RewriteRule directive either in your server.config or htaccess .
I understand that not all apache users have access to the main server.config file  but you don’t need to worry about it , if you are allowed to use htaccess then you can just use htaccess to manipulate your URLs.
To remove .php extension, you can use the following RewriteRule in server.config or htaccess.  Its 100% working code so you can just copy and paste it.

RewriteEngine on
#1) redirect /file.php to /file
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule (.+) /%1 [L,R=301]
#2) map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)/?$ /$1.php [L]

The rule above will change your php URL from




Explanation of the rule :
The first code block #1 redirects your URL http://example.com/file.php to  http://example.com/file as you can see the first rule redirects your original URL to a new URL without .php and   then the code block #2 internally maps the new URL to its original location ie /file to /file.php .
Both rule blocks handles the same request but the difference is that the first block makes a 301 external redirection  when you type the Original URL with .php . The second one then immediately internally  maps the new URL format to its existing one so that you can not get a 404 not found error. 

How useful was this post?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 20

No votes so far! Be the first to rate this post.