Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 754
John Roberts Avatar asked Oct 07 '14 15:10

John Roberts


People also ask

Do you need SSL for redirects?

Yes. The redirection is an HTTP-level action which happens inside the SSL envelope. The client needs to establish an SSL connection to the original host before it 'sees' the redirect, then after completing the redirect it must establish another SSL connection to the target host.

What is SSL redirect?

Once you have SSL installed, you need to redirect visitors who are still accessing your old HTTP site to your new HTTPS site. You can redirect visitors to your HTTPS domain automatically—even when they try to use your old HTTP domain.


1 Answers

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]
like image 78
Jon Lin Avatar answered Sep 28 '22 09:09

Jon Lin