Steve
Steve

Reputation: 1765

.htaccess redirect non-www and non-http to https://www

I'm trying to redirect:

to

using:

RewriteCond %{HTTP_HOST} ^example.extension$ [OR]
RewriteCond %{HTTPS_HOST} ^example.extension$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.extension$ [NC]
RewriteRule (.*) https://www.example.extension$1 [R=301,L]

http://example.extension is redirected to https://www.example.extension, however, I receive the error:

The page isn’t redirecting properly

From this answer, if I change my .htaccess rules to:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.extension%{REQUEST_URI} [R=301,L,NE]

the same symptom occurs, even after clearing the browser cache.

Also, I have some sub-domains I don't want to redirect to www, like:

I need http://*.example.extension redirected to https://*.example.extension

Other than the rewrite rules I am requesting help with, the only other content in .htaccess is Wordpress related:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress

# BEGIN MainWP
# END MainWP

Help appreciated.

Upvotes: 0

Views: 321

Answers (2)

Croises
Croises

Reputation: 18671

With CloudFlare, You must use CloudFlare Page Rules: https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-

But you can use:

RewriteEngine On

#main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

#sub-domains
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Upvotes: 1

anubhava
anubhava

Reputation: 784958

There is no variable called HTTPS_HOST.

Replace your rule with this rule:

RewriteEngine On

# main domain: add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# sub domain
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Make sure to clear your browser cache completely.

Upvotes: 2

Related Questions