Take Your Google Rank From Uff to Whoa!

.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.
.htaccess File Complete Tutorial For SEOs | Solve 7 SEO Issues 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…

New from Amit Tiwari

About the author

Amit Tiwari
Amit Tiwari
Amit Tiwari has been working as an SEO and Digital marketer for over a decade in various roles. Amit started the YouTube channel “Amit Tiwari” and founded the company “ClickLocal Digital Advertising”.

SEOphisticated Digital Advertising

About Us

We deliver unparalleled insights into the ever-evolving world of SEO.

Contact Us

Ready to take your digital presence to new heights? Looking for a touch.

Partnerships

Have a business proposition in mind? Start a partnership with us.

Search Engine Optimization

Improving your website visibility in search engine results.

Local SEO

Enhance the online visibility of your local businesses.

Technical SEO

Improve Your Website Speed, Visibility, Ranking, and User Experience.

Ecommerce SEO

It refers to the process of optimizing an online store or e-commerce website.

Google Penalty Recovery

Google penalty recovery service is a specialized offered by SEO experts.

Website Designing

A well-designed website boosts your online presence and credibility.

Local Business Website

Website for a local business in 2023 involves understanding.

E-Commerce Website

Ecommerce website involves incorporating the latest trends & technologies.

UX/UI Improvement

UI/UX improvements in website designing and SEO are essential.

Social Media Management

In 2023, the benefits of social media for businesses have become more vital than ever.

Video Production

Creating visual content for online platforms like websites, social media etc.

Advanced Google Ads Course

Most wide curriculum for Google Ads. Good fit for beginners and professionals both.

Local SEO Course

Our first course is about specializing in Local S.E.O. and ranking top in GMB.

GA 4 for SEO and PPC

This course is designed to give you complete command over GA 4 (Google Analytics 4).

Advanced SEO Course

Understanding and practical knowledge of critical aspects of SEO.

Basic SEO Course

In this course we cover all about understanding the basics of SEO

Local SEO Plan

Select the best for your business and get in touch with us today!

Website SEO Plan

We don’t just rank you, we get you the traffic that converts.