2

I want to redirect all my website pages to https and www. In order to do that I'm using the rules below and it's working fine.

# Rewrite to WWW with HTTPS
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.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However, Google PageSpeed Insights suggest me to "avoid landing page redirects. Your page has 2 redirects." - https://developers.google.com/speed/docs/insights/AvoidRedirects

Any suggestion to do this in a better way?

  • To avoid the 2 redirects mentioned you simply have to reverse those two rule blocks (ie. redirect to https://www first). Also, the NC should not be used on that RewriteCond directive (where the CondPattern is negated) - you don't want a case-insensitive match there and it only creates more work. (Just curious, where did that code come from, it looks familiar?) – MrWhite Jul 24 '17 at 23:20
  • @MrWhite stackoverflow.com/a/13997498/673167 – notGeek Dec 20 '17 at 12:40
2

This will reduce the redirects

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
  • 1
    Minor point, but... You shouldn't include the NC flag on a negated condition against HTTP_HOST, otherwise, it won't redirect malformed requests that contain uppercase letters (which it should). – MrWhite Jul 24 '17 at 23:16
  • @MrWhite your suggestion is just to remove the flag or to replace with another one? thanks – notGeek Dec 20 '17 at 12:09
  • @notGeek Yes, just remove it. – MrWhite Dec 20 '17 at 12:34
0

There are some ways to rewrite the .htaccess file (for instance see this question). But, there is a simpler way to solve this: make sure you don't require people to go through the redirects to arrive at your page. That is, if you want people to see the https/www version of the page, then get them to the https/www version instead of the http/non-www version. The problem isn't so much the double redirects as it is the bad experience and slow speeds the double redirects causes.

So, how do you get people to the https/www version instead of the http/www version? That would require things like linking to that https/www version from your ads or social shares, making that the URL in your XML sitemap, specifying that URL in canonical tags, using the https/www version of the URLs in your internal site links, etc. By doing this, you'll cut down on the number of times people will have to go from http -> https AND go from non-www -> www, which will make the experience faster and better.

  • "there is a simpler way to solve this" - The redirect is still required for the edges cases when third parties incorrectly link to the non-canonical URL. And for when sites are later migrated from HTTP to HTTPS (or even non-www to www) after the site has already been indexed and linked to. – MrWhite Jan 21 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.