Redirect to WWW with HTTPS optimization
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, https, mod-rewrite, google-pagespeed, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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?
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]
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.
Comments
Post a Comment