John Roberts
John Roberts

Reputation: 5986

Avoiding Landing Page Redirects with SSL

I recently did a Google PageSpeed analysis of my website and received the following message:

Avoid landing page redirects

Your page has 2 redirects. Redirects introduce additional delays before the page can be loaded.

Avoid landing page redirects for the following chain of redirected URLs.

http://example.net/

https://example.net/

https://www.example.net/

Is there anything I can do about this (like modifying my htaccess file in some way), or is this an unavoidable consequence?

Here is my htaccess just in case:

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Upvotes: 7

Views: 7741

Answers (1)

Jon Lin
Jon Lin

Reputation: 143926

You can combine the two redirects into one by using the OR flag

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

Upvotes: 6

Related Questions