How to Remove .html, .php, or Both from URLs

How to Remove .html, .php, or Both from URLs

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.

  1. Go to your website’s root directory (e.g., public_html).
  2. 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.htmlyourdomainname.com/about
  • yourdomainname.com/contact.htmlyourdomainname.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.phpyourdomainname.com/about
  • yourdomainname.com/contact.phpyourdomainname.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.htmlyourdomainname.com/about
  • yourdomainname.com/about.phpyourdomainname.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)

✅ 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:

Picture of James Wilson

James Wilson

For over 10 years, James has been working in the tech industry. He's an expert in areas like software development, cybersecurity, and cloud computing. He understands the challenges and opportunities that new tech companies face, and he's known for coming up with creative solutions to help them succeed.

Leave a Reply

Enjoy 20% Off Your First Order!

Use promo code WELCOME at checkout to claim your exclusive discount.

Get 20% OFF on your first order

Oh hi there! It’s nice to meet you.

Please Kindly, sign up to receive awesome content in your inbox, every month.