How to Remove .html and .php File Extensions from URLs
Having clean and user-friendly URLs is essential for improving website navigation and SEO. By removing file extensions like .html and .php, you can create shorter, more readable URLs that enhance user experience and look more professional. Additionally, search engines prefer concise URLs, which can contribute to better rankings.
Table of Contents
In this guide, weβll walk you through the step-by-step process to remove file extensions from URLs using .htaccess. Whether you want to remove only .html, only .php, or both, we’ve got you covered.
Hereβs what weβll cover:
β
How to remove .html file extensions
β
How to remove .php file extensions
β
How to remove both .html and .php extensions for a cleaner URL structure
By the end of this guide, your website URLs will look cleaner, more professional, and easier to remember! π
πΉ Step 1: Check if .htaccess Exists
Before making changes, ensure your website is running on an Apache server with mod_rewrite enabled.
- Go to your websiteβs root directory (e.g.,
public_html). - Look for a file named
.htaccess.- If it exists, edit it.
- If not, create a new file named
.htaccess.
πΉ Step 2: Remove .html from URLs
To remove .html extensions and make URLs cleaner, add the following code to your .htaccess file:
RewriteEngine On
# Redirect .html URLs to clean URLs (for SEO)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html\sHTTP [NC]
RewriteRule ^ %1 [R=301,L]
# Serve .html files without typing .html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
π Example:
yourdomainname.com/about.htmlβyourdomainname.com/aboutyourdomainname.com/contact.htmlβyourdomainname.com/contact
πΉ Step 3: Remove .php from URLs
To remove .php extensions, add this to your .htaccess file:
RewriteEngine On
# Redirect .php URLs to clean URLs (for SEO)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\sHTTP [NC]
RewriteRule ^ %1 [R=301,L]
# Serve .php files without typing .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
π Example:
yourdomainname.com/about.phpβyourdomainname.com/aboutyourdomainname.com/contact.phpβyourdomainname.com/contact
πΉ Step 4: Remove Both .html and .php
If your website has both .html and .php files and you want to remove both extensions, use this:
RewriteEngine On
# Redirect .html and .php URLs to clean URLs (for SEO)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.(html|php)\sHTTP [NC]
RewriteRule ^ %1 [R=301,L]
# Serve files without .html or .php in the URL
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
π Example:
yourdomainname.com/about.htmlβyourdomainname.com/aboutyourdomainname.com/about.phpβyourdomainname.com/about
πΉ Step 5: Update Internal Links
Now that your URLs no longer require .html or .php, it’s essential to update all internal links throughout your website.
β
Manually Update Links β Go through your HTML, PHP, or database links and remove .html or .php where necessary.
β Update Navigation Menus β If your website has navigation menus or buttons linking to pages with file extensions, modify them to use the new clean URLs.
β Check Dynamic Links β If your site dynamically generates links (e.g., via a CMS or PHP scripts), ensure they point to URLs without file extensions.
By updating all internal links, you ensure a smooth browsing experience and avoid broken links.
- Change this:
<a href="about.html">About</a>
- To this:
<a href="about">About</a>
πΉ Step 6: Clear Cache and Test
- Clear your browser cache.
- Restart Apache (if needed):
systemctl restart apache2 # Ubuntu/Debian
systemctl restart httpd # CentOS/RHEL
- Test your new URLs!
β Conclusion
By following these steps, you have successfully removed .html and .php file extensions from your URLs, making them cleaner and more user-friendly. This small yet impactful change enhances your website’s SEO, improves user experience, and makes your links easier to read and remember.
Clean URLs contribute to a more professional web presence, and search engines tend to favor shorter, keyword-rich URLs. Now, your website looks more modern, structured, and optimized for both visitors and search engines. π
If you have any questions or need further assistance, let us know in the comments or feel free to reach out to Aveshost Support!
Happy optimizing! π
Suggested Reading:
- How to Install an SSL Certificate in cPanel
- How to Flush DNS Cache on Windows, Mac, Linux & Browsers
- How to Set Up a MySQL Database & User in cPanel (2 Easy Methods)
- How to Buy cPanel Hosting for Your Website: Beginnerβs Guide
- How to Set Up a PostgreSQL Database and User in cPanel
Frequently Asked Questions (FAQs)
Why should I remove .html and .php extensions from my URLs?
Removing file extensions creates cleaner, shorter, and more readable URLs that improve user experience, look more professional, and are preferred by search engines for better SEO rankings.
What do I need before removing file extensions from URLs?
You need an Apache web server with mod_rewrite enabled and access to your website’s root directory to create or edit the .htaccess file.
How do I remove both .html and .php extensions at once?
Add specific rewrite rules to your .htaccess file that redirect both .html and .php URLs to clean versions, then update all internal links throughout your website to remove the extensions.
What should I do after removing file extensions from URLs?
Update all internal links (HTML, PHP, navigation menus, and dynamic links), clear your browser cache, restart Apache if needed, and thoroughly test your new URLs to ensure they work properly.