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.
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/about
yourdomainname.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/about
yourdomainname.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/about
yourdomainname.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!
Frequently Asked Questions (FAQs)
.html
or .php
from my URLs?Removing file extensions makes URLs cleaner, improves SEO, and enhances user experience. It also allows for easier URL structure changes in the future without breaking links.
.html
or .php
extensions?No, as long as you correctly implement the .htaccess
rules and update internal links to reference URLs without extensions, your website will function normally.
No, you don’t need to rename your .html
or .php
files. The .htaccess
rules ensure they are still accessible without the extensions.
No, but to avoid broken links, use 301 redirects in .htaccess
. This will ensure search engines and users are redirected to the new URLs seamlessly.
.html
and .php
at the same time?Yes, you can modify your .htaccess
file to support both extensions, allowing visitors to access pages without typing .html
or .php
.
This method works on Apache servers with mod_rewrite
enabled. If you’re using NGINX, you’ll need to configure URL rewriting in the NGINX configuration file instead.
After updating your .htaccess
file, clear your browser cache and try accessing your pages without the .html
or .php
extension.
✅ 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: