Single redirect hop http to https & non-www to www (htaccess)

Sitebee

Technical SEO
Staff member
Here is the solution to set a single redirect hop for http to https & non-www to www using a mode_rewrite htaccess directive. This is useful for removing the redirect chain often caused by http & non-www requests.

Typically if a request non-ssl non-www is made, the first hop is forced ssl, then a second hop is made for the non-www to www. Resulting in 2 hops. Tools such Screaming Frog will pick this up as a redirect chain.

The htaccess directive below should eliminate the secondary hop. The requests for non-ssl non-www should force ssl and www in a single hop.

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
</IfModule>

Remember to remove any existing directives that force ssl and www. Otherwise you might see conflicting directives. Feel free to comment below is you need a helping hand.