How to remove .php and .html from URLs using .htaccess
John Mwaniki / Updated on 07 Jul 2024When you create web pages, they will by default have file extensions (such as .php or .html) in their URLs.
Examples:
http://www.example.com/about.html
http://www.example.com/about.php
However, some developers prefer to remove these extensions to create cleaner and more user-friendly URLs.
Examples:
http://www.example.com/about
http://www.example.com/contact
This is also very common with websites built with Content Management Systems(CMS) such as WordPress, or with frameworks (such as Laravel).
Some of the reasons why people do it include:
- To make the URLs user-friendly and easier to remember.
- To hide the back-end programming language of the website from end users. For experts, it's not hard to still identify the language used.
- It is easy to change backend technology without affecting the URL. For instance, a page can change its file name from .html to .php without affecting the URL.
It is possible to achieve this using the .htaccess
file.
In this article, I'll guide you on how to remove .php and .html extensions from URLs using .htaccess.
What is .htaccess?
The .htaccess (Hypertext Access) file is a configuration file used by Apache-based web servers to modify the behavior of web requests.
It is commonly used to perform tasks such as URL rewriting, access control, and caching among others. The .htaccess file is usually placed in the root directory of the website, and it contains directives that specify how the web server should handle requests.
However, it is a directory-level configuration file meaning that you can place it in the specific directory that you want it to affect instead of the root directory. It affects all the files and subdirectories within the directory where it is placed.
Note that the .htaccess file is a hidden file and may not be visible by default in your website directories. In case you don't see any file which has its name starting with a dot (.), you will need to first enable the showing of hidden files.
Removing .php from URLs
To remove the ".php" extension from URLs, we need to use the RewriteRule
directive in the .htaccess file. The following code will remove the .php extension from URLs:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Breaking down the code:
- The first line, RewriteEngine On, enables the rewriting engine. It should appear only once in the entire .htaccess file. If you find the file has it when adding your code, omit it and make sure it appears before any rewrite line.
- The second line, RewriteCond %{REQUEST_FILENAME} !-d, checks if the requested URL is not a directory.
- The third line, RewriteCond %{REQUEST_FILENAME}\.php -f, checks if the requested URL with the .php extension exists as a file.
- The fourth line, RewriteRule ^(.*)$ $1.php [L], rewrites the URL by removing the .php extension and appending it to the end of the URL.
Though the above code will allow access to URLs without file extensions eg "http://www.example.com/about" for the "about.php" file, users will still be able to access the URL with file extension (http://www.example.com/about.php) without getting redirected to "/about".
You definitely don't want two versions of the same URL. To force redirection whenever a user visits the URL with an extension, you will need to add two extra lines to the .htaccess file to have the full code as below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule ^ /%1 [NC,L,R]
The "RewriteCond %{THE_REQUEST} /([^.]+).php [NC]" directive checks whether a URL path ends in ".php" (excluding subdirectories with a dot in their name) and captures the part of the path before ".php".
The "RewriteRule ^ /%1 [NC,L,R]" directive then issues a redirect response to the client with the captured path before ".php".
Removing .html from URLs
The approach to this is very similar to the one used for removing the .php extension above. The following code will remove the ".html" extension from URLs and force a redirection to the URL version without the extension in case a user types the full URL including the extension.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{THE_REQUEST} /([^.]+).html [NC]
RewriteRule ^ /%1 [NC,L,R]
The only difference with the one for PHP is that this checks for the .html extension instead of the .php extension.
Removing both .php and .html from URLs
If your website comprises PHP and HTML files, you can remove both ".php" and ".html" extensions from URLs by combining the two previous solutions. The following code will remove both extensions from URLs:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{THE_REQUEST} /([^.]+).html [NC]
RewriteRule ^ /%1 [NC,L,R]
Conclusion
Removing ".php" and ".html" extensions from URLs is a common practice that makes URLs more user-friendly and easier to remember.
In this article, you have learned how to rewrite URLs to remove file extensions using the RewriteRule
directive in the .htaccess file.