How to Remove .html or .php Extensions from URLs Using .htaccess


Removing file extensions like .html or .php from URLs improves readability and makes your website more user-friendly. This guide will show you how to configure your .htaccess file to achieve clean, extension-less URLs.

Why Remove URL Extensions?

Cleaner URLs offer several benefits:

  • Improves user experience by making URLs easier to remember.
  • Enhances SEO by creating more readable and concise URLs.
  • Makes it easier to switch technologies without affecting existing URLs.

Removing .html Extension

To remove the .html extension from URLs and make them clean, you need to configure your .htaccess file. This ensures users can access pages without explicitly typing the .html extension, while the server still serves the correct files.

Apache
# Enable Rewrite Engine
RewriteEngine On

# Remove .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]

# Redirect .html to extensionless URL
# To externally redirect /dir/about.html to /dir/about
RewriteCond %{THE_REQUEST} \s/(.*)\.html [NC]
RewriteRule ^ %1 [R=301,L]

Expected Output:

  • Before:

    • https://example.com/about.html
    • https://example.com/contact.html
  • After:

    • https://example.com/about
    • https://example.com/contact

Removing .php Extension

To remove the .php extension from URLs, follow a similar approach. Users will now see cleaner URLs without the .php extension while the server processes requests to the .php files seamlessly.

Apache
# Enable Rewrite Engine
RewriteEngine On

# Remove .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

# Redirect .php to extensionless URL
RewriteCond %{THE_REQUEST} \s/(.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

Expected Output:

  • Before:

    • https://example.com/about.php
    • https://example.com/contact.php
  • After:

    • https://example.com/about
    • https://example.com/contact

.htaccess Code for Both .html and .php Removing Extension

This setup removes the extensions and redirects URLs containing .html or .php to their extension-less counterparts.

Apache
# Enable Rewrite Engine
RewriteEngine On

# Redirect .html URLs to extension-less
RewriteCond %{THE_REQUEST} \s/+(.+)\.html [NC]
RewriteRule ^ %1 [R=301,L]

# Redirect .php URLs to extension-less
RewriteCond %{THE_REQUEST} \s/+(.+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Remove .html extension for internal requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ $1.html [L]

# Remove .php extension for internal requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ $1.php [L]

The R=301 flag ensures a permanent redirect, preserving SEO rankings and preventing duplicate content issues.

Removing URL Extensions for Specific Directories

Removing URL extensions for specific directories can be useful for improving website organization and user experience. To remove URL extensions for a specific directory, you can use the following code snippet:

Apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^directory/(.*)$ directory/$1.html [L]

This code snippet removes URL extensions for the /directory directory. The RewriteRule directive specifies the pattern to match and the substitution to make.

Replace directory/ add your folder directory path

Common Errors and Troubleshooting

When removing URL extensions, you may encounter errors or issues that prevent the functionality from working correctly. Here are some common errors and troubleshooting tips:

  • 404 Not Found: This error occurs when the server is unable to find the requested file or directory. To resolve this issue, check the file system and ensure that the requested file or directory exists.
  • 500 Internal Server Error: This error occurs when there is a problem with the server configuration or the .htaccess file. To resolve this issue, check the server logs and ensure that the .htaccess file is correctly configured.

Removing .asp Extension

To remove the .asp extension from URLs and make them more readable, you can use .htaccess rules. This configuration helps maintain clean URLs while still serving .asp files.

To remove URL extensions for a specific file type, you can use the following code snippet:

Apache
# Enable Rewrite Engine
RewriteEngine On

# Remove .asp extension
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*)$ $1.asp [L]

# Redirect .asp to extensionless URL
RewriteCond %{THE_REQUEST} \s/(.*)\.asp [NC]
RewriteRule ^ %1 [R=301,L]

Output:

  • /about.asp/about
  • /contact.asp/contact

Removing .jsp Extension

For .jsp files, removing the extension using .htaccess rules is straightforward and can enhance your site URL structure by making it extension less.

Apache
# Enable Rewrite Engine
RewriteEngine On

# Remove .jsp extension
RewriteCond %{REQUEST_FILENAME}.jsp -f
RewriteRule ^(.*)$ $1.jsp [L]

# Redirect .jsp to extensionless URL
RewriteCond %{THE_REQUEST} \s/(.*)\.jsp [NC]
RewriteRule ^ %1 [R=301,L]

Output:

  • /about.jsp/about
  • /contact.jsp/contact

Preparation and Key Considerations

Before setting up URL rewrite rules, ensure the following prerequisites are met to avoid potential issues:

  1. mod_rewrite is Enabled:
    Confirm that the mod_rewrite module is active on your web server. This is essential for processing .htaccess rewrite rules.

  2. Access to Modify .htaccess Files:
    Ensure you have the necessary permissions to create or edit .htaccess files in your website's root directory.

  3. Thorough Testing:
    Test the configuration thoroughly to avoid broken links or unexpected errors. Use a staging environment if possible before applying changes to your live website.

How to Set Up the .htaccess File to Remove URL Extensions

Setting up an .htaccess file to remove URL extensions like .html or .php can enhance your website’s appearance and improve SEO. Follow these steps to locate, create, or edit your .htaccess file and configure it properly.

Steps to Set Up the .htaccess File

  1. Access Your Website Root Directory:
    • Use an FTP client (e.g., FileZilla) or your hosting provider's file manager to navigate to the root directory of your website.
    • For most hosting setups, this is typically the /public_html directory.
    • On local servers, the root directory is often found in /var/www/html.
  2. Check for an Existing .htaccess File:
    • Look for a file named .htaccess (note: it starts with a dot and has no file extension).
    • If it exists, open the file using a plain text editor such as Notepad++, VS Code, or your hosting provider's built-in editor.
    • Add the necessary rewrite rules to the file and save your changes.
  3. Create a New .htaccess File (if it does not exist):
    • If no .htaccess file is present, create a new file named .htaccess using a plain text editor.
    • Ensure the file has no extension (e.g., .txt or .htm).
    • Add the required rewrite rules to remove extensions and save the file.
  4. Upload and Verify the File Name:
    • Confirm the file is named .htaccess (without any additional extensions).
    • If you created the file locally, upload it to your website root directory.
  5. Test the Configuration:
    • Open a browser and navigate to a URL with the original extension (e.g., https://example.com/about.html).
    • Verify that it redirects to the clean version of the URL (e.g., https://example.com/about).

Troubleshooting Tips

  • 500 Internal Server Error:
    • Double-check the syntax of your .htaccess file.
    • Ensure the file uses UNIX line endings (especially if created on Windows).
  • Clean URLs Not Working:
    • Verify that your server supports .htaccess overrides.
    • Ensure the Apache configuration includes the directive AllowOverride All for your website directory.

Here are common errors when removing URL extensions with .htaccess:

  1. mod_rewrite Not Enabled: The server may lack mod_rewrite, resulting in .htaccess rules not working.
  2. Incorrect .htaccess Placement: The file must be in the correct directory.
  3. Syntax Errors: Typos or missing directives in .htaccess can break the configuration.
  4. Caching Issues: Old rules might persist due to browser or server caching.
  5. Conflicting Rules: Multiple .htaccess files or overlapping directives may cause unexpected behavior.

Practical Applications of URL Rewriting

Using .htaccess to remove URL extensions is especially beneficial for:

  1. Static HTML Websites:
    Simplify URLs for static pages by removing file extensions like .html.

  2. PHP-Based Websites:
    Hide .php extensions to make URLs cleaner and more user-friendly.

  3. Enhancing URL Aesthetics:
    Improve the readability and professionalism of your website URLs.

  4. Boosting SEO Performance:
    Search engines favor clean, concise URLs, which can positively impact your rankings.