Download Official App





htaccess File Complete Tutorial For SEOs

Solve 7 SEO issues such as www to non-www & vice versa, fasten the website, add/remove trailing slash with .htaccess file.

In a rush? Download the code: https://amittiwari.net/wp-content/uploads/2023/04/htaccess-code-file.pdf

HTTP to HTTPS Redirect

The first task is to redirect HTTP URLs to HTTPS URLs. Copy the provided code and paste it into your .htaccess file. Replace example.com with your domain name. This will redirect HTTP URLs to HTTPS URLs using a 301 redirect, as specified in the code.

RewriteEngine On
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

WWW to Non-WWW Redirect

The second task is to redirect non-www URLs to www URLs or vice versa. Again, replace example.com with your domain name and copy and paste the provided code into your .htaccess file. The code checks the domain name and adds www to URLs that do not have it.

www to non-www

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

non-WWW to WWW

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Add/remove trailing slash

The third task is to add or remove trailing slashes from URLs. Although these two URLs may look the same, Google considers them as two different URLs. You can choose to use URLs with or without trailing slashes, but it is essential to stick to one pattern. The code provided will help you achieve this task.

Add trailing slash

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

Remove trailing slash

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)/$ $1 [L,R=301]

Redirect one page to another

To redirect a URL, simply paste one line of code into your .htaccess file that includes both the old and new URLs. This simple solution can help you avoid using any plugins and can save you time and effort.

Redirect 301 /old-page https://example.com/new-page

Remove extension (.html, .php. or .aspx) from url

If you want to remove .html or .aspx from the end of your URL and give it a clean look, you can use this code. This code is for removing .html, but if you want to remove .php or .aspx instead of .html, just replace it and it will work.

RewriteEngine on

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]

RewriteRule ^ /%1 [NC,L,R=301]

RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^ %{REQUEST_URI}.html [NC,L]

Rewrite these type URLs

You can use this code to make these types of URLs user-friendly.

Old URL: example.com/article.php?id=123

New URL: example.com/article/123

Now, this change is a bit tricky because the code will change based on the pattern of these URLs, but still, you get an idea that it’s possible.

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^article/([^/]+)$ /article.php?id=$1 [L]

Custom 404 page

.htaccess file can also be used to assign a custom page as a 404 error page. You don’t need any plugins for this, simply create a custom page on your website and paste the code into your .htaccess file, replacing the example URL with your own.

ErrorDocument 404 https://example.com/custom-404

Increase the speed of the website

You can use the .htaccess file to increase your website’s speed by implementing the browser cache method. Paste the code into your .htaccess file, and your website will load faster for returning visitors.

<filesMatch “.(css|jpg|jpeg|png|gif|js|ico)$”> 

Header set Cache-Control “max-age=2592000, public”

 </filesMatch>

What we learned so far:

The .htaccess file is a powerful tool that can help you save a lot of work and headache in solving these issues.

Do you have any other interesting use cases of .htaccess file? Please let us know in the comments here…

Welcome to Amit Tiwari’s official website, where we deliver unparalleled insights into the ever-evolving world of search engine optimization (SEO).
Copyright © 2024 All rights reserved.
A ClickLocal Digital Advertising LLP Product.