How to Remove .php, .html, .htm Extensions Using .htaccess
Want cleaner, more professional-looking URLs for your website? Stripping file extensions like .php
, .html
, or .htm
not only improves SEO and aesthetics, but it also future-proofs your site’s URLs. In this tutorial, we’ll show you how to use .htaccess
to hide file extensions from your URLs on Apache servers.
Prerequisites
- Your site is hosted on an Apache server
- You have mod_rewrite enabled (usually enabled by default)
- Basic knowledge of FTP or cPanel file manager
Step 1: Create or Edit Your .htaccess File
- Go to your website’s root directory (usually
public_html
). - Look for a file named
.htaccess
. If it doesn’t exist, create a new file and name it .htaccess
.
Important: Make sure your text editor doesn’t save it as .htaccess.txt
.
Step 2: Enable mod_rewrite
Add the following code at the top of your .htaccess
file:
RewriteEngine On
Step 3: Remove .php Extension
Add this rewrite rule:
# Remove .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
Example: https://example.com/about
loads about.php
Step 4: Remove .html and .htm Extensions
Add these rules below the previous one:
# Remove .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.html [L]
# Remove .htm extension
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^([^/]+)/?$ $1.htm [L]
Examples:
https://example.com/contact
→ contact.html
https://example.com/info
→ info.htm
Step 5: Redirect Visitors from Extension URLs to Extension-less URLs (Optional)
This will redirect old URLs like about.php
to /about
using a 301 redirect:
# Redirect .php to extension-less
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect .html to extension-less
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect .htm to extension-less
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.htm [NC]
RewriteRule ^ %1 [R=301,L]
Step 6: Test Your URLs
- Visit
https://yourdomain.com/about
- Make sure it loads the corresponding
about.php
, about.html
, or about.htm
file - Old links should redirect correctly if step 5 is included
Troubleshooting
- 500 Internal Server Error? Check for typos and ensure
AllowOverride All
is set in Apache config. - Changes not working? Clear your browser cache and confirm
mod_rewrite
is enabled.
Final Thoughts
Removing file extensions using .htaccess
makes your URLs look clean, professional, and SEO-friendly. With just a few lines of code, you can enhance your website's usability and maintainability.
Got Questions?
Drop them in the comments or contact me directly. Happy coding!