Mobile app version of vmapp.org
Login or Join
Candy875

: Redirect to WWW with HTTPS optimization 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

@Candy875

Posted in: #GooglePagespeed #Htaccess #Https #ModRewrite

I want to redirect all my website pages to https and 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 here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use # [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .* www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


However, Google PageSpeed Insights suggest me to "avoid landing page redirects.
Your page has 2 redirects." -
developers.google.com/speed/docs/insights/AvoidRedirects
Any suggestion to do this in a better way?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Candy875

2 Comments

Sorted by latest first Latest Oldest Best

 

@Hamaas447

This will reduce the redirects

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ www.example.com/ [L,R=301]

10% popularity Vote Up Vote Down


 

@Alves908

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme