How to Remove .html from URLs on Your Website

AYUSH KUSHWAHA
1
How to Remove .html from URLs on Your Website

How to Remove .html from URLs on Your Website

Why Remove .html from URLs?

Removing .html from your URLs can make them look cleaner and more user-friendly. This practice can also help improve your website's SEO by making the URLs more readable and easier to remember. For example, example.com/about looks more professional and modern than example.com/about.html.

How to Remove .html from URLs

There are several ways to remove .html from your URLs, depending on the server and the technology stack you are using. Here, we'll cover how to do this for Apache, Nginx, and using an .htaccess file.

Using Apache

If your website is hosted on an Apache server, you can use the .htaccess file to rewrite the URLs.

  1. Create or Edit the .htaccess File:

    Locate the .htaccess file in the root directory of your website. If it doesn't exist, create one.

  2. Add the Rewrite Rules:

    Open the .htaccess file and add the following code:

    
    
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html [L]
    
    
    
                

    This code tells the server to look for a file with the .html extension if the URL without the extension is requested.

  3. Save and Upload:

    Save the changes to the .htaccess file and upload it to your server.

Using Nginx

For websites hosted on an Nginx server, you can achieve the same result by editing the server configuration file.

  1. Edit the Server Block:

    Open your Nginx configuration file (usually located at /etc/nginx/sites-available/your_site).

  2. Add the Rewrite Rules:

    Inside the server block, add the following code:

    
    
    
    location / {
    
        try_files $uri $uri.html $uri/ =404;
    
    }
    
    
    
                

    This code tells Nginx to try accessing the URL as it is, then with a .html extension, and finally as a directory.

  3. Restart Nginx:

    Save the changes and restart Nginx to apply the new configuration:

    
    
    
    sudo systemctl restart nginx
    
    
    
                

Using HTML and JavaScript

If you don't have access to the server configuration files or prefer a simpler approach, you can use HTML and JavaScript to achieve a similar effect.

  1. Create a Redirect Script:

    Add the following script to the <head> section of your HTML files:

    
    
    
    <script>
    
      if (window.location.pathname.endsWith(".html")) {
    
        window.location.pathname = window.location.pathname.replace(".html", "");
    
      }
    
    </script>
    
    
    
                

    This script automatically redirects users from URLs with .html to the cleaner version without it.

Conclusion

Removing .html from your URLs can enhance the user experience and improve your website's SEO. Depending on your server setup, you can achieve this using Apache, Nginx, or even a simple JavaScript solution. Choose the method that best fits your needs and make your website URLs clean and professional.

Post a Comment

1Comments

  1. Thanks a lot it's really helpful 😃

    ReplyDelete
Post a Comment