Mobile app version of vmapp.org
Login or Join
Shanna517

: Https redirects + Cloudflare Page Rules missing situation I have just moved my website to https and after some scares it is working, mostly. I did find out that when I input the naked domain

@Shanna517

Posted in: #Cloudflare #Https #Redirects

I have just moved my website to https and after some scares it is working, mostly.

I did find out that when I input the naked domain on the browser it is not going to the desired URL, as follows:


typed URL = example.com
redirects to = example.com desired URL = www.example.com

The following Page Rule is set on Cloudflare:


pattern = *example.com/* rule = Forwarding to www.example.com/

On the origin server, the .htaccess file has:

RewriteCond %{HTTPS} off

RewriteRule (.*) %{HTTP_HOST}%{REQUEST_URI} [R=301,L]


What am I missing?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

%{HTTP_HOST} will refer to whatever host the site was accessed through. ie. example.com or example.com. To always redirect to example.com then simply put this in the substitution:

RewriteCond %{HTTPS} off
RewriteRule .* www.example.com%{REQUEST_URI} [R=301,L]


But unless you had a similar rule before then both example.com and example.com would have been accessible (and possibly indexed). In fact, if you access example.com now it does not redirect to www.example.com. You also need the following, in addition to the above:

RewriteCond %{HTTP_HOST} !www.
RewriteRule .* www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


So, in summary, the first block corrects for HTTP -> HTTPS and the second corrects if the www subdomain is omitted. I also removed the parentheses in your pattern (.*) since you aren't capturing it for the substitution.

These two rule blocks could be combined into one if desired...

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !www.
RewriteRule .* www.example.com%{REQUEST_URI} [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme