Reputation: 121
I've seen a few questions like this on here, but no answer seems to work. I just can't seem to figure this out. Here is my code:
# enable basic rewriting
RewriteEngine on
# enable symbolic links
Options +FollowSymLinks
# Primary Domain - Force the use of "https"
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Primary Domain - Force the use of "www"
RewriteCond %{http_host} ^example.org [nc]
RewriteRule ^(.*)$ https://www.example.org/$1 [r=301,nc]
# Primary Domain - .com to .org
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
# Primary Domain - .net to .org
RewriteCond %{HTTP_HOST} ^example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
So whenever I go to www.example.org
or http://www.example.org
it will NOT redirect. This is what is frustrating me. If I go to example.org or any of the other redirects, it works though. Am I setting this up wrong?
Also, where it says "HTTP_HOST", do I leave it as is? Like do I replace it with my http host?
Upvotes: 3
Views: 1869
Reputation: 24478
Amie if you want to force www
and https
both you can check for the absence of www. or check if https
is not On
then redirect. So either way it will redirect. example.
# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
Replace mydomain.org with your domain.
Then you can have a htaccess file like this. I took the last two rules and combined them so you have a smaller htaccess file.
# enable basic rewriting
RewriteEngine on
# enable symbolic links
Options +FollowSymLinks
# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
# Primary Domain - .com to .org Or .net to .org
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.(net|com)$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
Also, where it says "HTTP_HOST", do I leave it as is? Like do I replace it with my http host?
%{HTTP_HOST}
is a variable that contains the host sent with Http headers from the browser. So if someone requests http://mydomain.org/somepage.php
that variable %{HTTP_HOST}
will contain mydomain.org
. You don't change it. You can use it to match things or set conditions like in the rules. Hope that answers your question.
Upvotes: 1