Aminesrine
Aminesrine

Reputation: 2140

How to avoid a redirect chain

SSL & SEO Report of my-website says:

http://my-website.com/ redirects to https://www.my-website.com/ through a redirect chain. This hurts your rankings

http://my-website.com/ --> https://my-website.com/ --> https://www.my-website.com/

How to avoid this redirect chain via .htaccess?

My actual htaccess contains:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-website.com/$1 [R=301,L]

Upvotes: 0

Views: 955

Answers (1)

Joe
Joe

Reputation: 4897

Change your .htaccess code to force HTTPs and WWW to this:

RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.my-website.com%{REQUEST_URI} [R=301,L]

This combines your rules and will stop them from chaining. The first condition checks if HTTPs is not on and the second condition checks if www is present or not.

If either of those conditions fail, it will rewrite the URL using 301 redirection to use both HTTPs and www.

Make sure you clear your cache before testing it.

Upvotes: 2

Related Questions