.htaccess file does not redirect http: // www. https: // www
I made a .htaccess file to redirect all site traffic to https://www.
.
This is my complete .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The following redirects work exactly as expected:
http://example.com -> https://www.example.com
https://example.com -> https://www.example.com
https://www.example.com -> https://www.example.com
With the exception of:
http://www.example.com -> http://www.example.com
As shown above, if you go to http://www.
, it doesn't redirect to the HTTPS version.
Can someone help me understand why the other redirects are working fine but they are not?
Additional notes. I've looked at several posts on StackOverflow, but most of their solutions end up with redirect loop errors.
source to share
After contacting 123-Reg (my hosting provider), they presented this solution, which works great:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
They basically set up a script to do two things: change the domain to WWW
, if not already set, change THEN to HTTPS
. Also, they used ENV:HTTPS
which is different from what was found in their documentation ( ENV:SSL
).
Glad to figure this out and maybe it will help others using 123-Reg Hosting.
source to share
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
As you discovered, this will not redirect when requesting the canonical hostname (i.e. www.example.com
) regardless of whether it is HTTP or HTTPS.
You need to change this to something like:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
This will redirect to the canonical URL if HTTPS is "disabled" or if it is not the canonical hostname.
... but most of their solutions end up with redirection loop errors.
If you are behind a proxy (such as CloudFlare) that manages your SSL certificate, this can lead to a redirect loop as the connection between you and the proxy can be HTTP, not HTTPS. This means that your server only serves content over HTTP, not HTTPS. If so, then there are additional headers that can be checked on demand (eg X-Forwarded-Proto
) or set a "page rule" in the case of CloudFlare (a flexible service without SSL).
UPDATE # 1: 123-Reg provides an SSL reference document . They seem to have set an environment variable SSL
when "the connection is SSL secured". This would mean that you could do something like the following:
RewriteCond %{ENV:SSL} ^$ [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
This is, however, non-standard and "unique" to 123-Reg. (Other than that: the PHP code suggestion in the linked document 123-Reg is not the recommended way to check an environment variable, as it will result in E_NOTICE if the variable is not set !?)
You should also make sure the browser cache is cleared before testing.
UPDATE # 2: To help with debugging ... to see what values are returned you can assign some Apache values to environment variables and check their values on the server side script (like PHP?). For example:
RewriteCond %{HTTPS} (.*)
RewriteRule ^ - [E=APACHE_HTTPS:%1]
# You don't really need this, but for completeness...
RewriteCond %{ENV:SSL} (.*)
RewriteRule ^ - [E=APACHE_SSL:%1]
RewriteCond %{HTTP:X-Forwarded-Proto} (.*)
RewriteRule ^ - [E=APACHE_PROTO:%1]
Then check your environment variables APACHE_HTTPS
, APACHE_SSL
and APACHE_PROTO
server side script. (for example, in PHP, use getenv () .)
source to share
After a lot of 123 Reg issues and redirecting all page versions to one corresponding https page using Wordpress, this is what works for me on multiple sites right now and has proven to be effective from an SEO standpoint. Hope this helps!
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
source to share