How to edit htaccess to redirect all trafic to secure www domain - apache

How to achieve the following:
redirect all http:// domain .com to https:// www.domain .com
redirect all http:// www.domain .com to https:// www.domain .com
redirect all https:// domain .com to https:// www.domain .com
So basically, all my traffic be it non-www or www will be redirected to SSL www domain.
Edit:
Additionally, I have 2 subdomain that works on http, so I want to achieve the above and also keep the 2 subdomain free from the above redirect rule.

You can use the following code in Root/.htaccess :
RewriteEngine on
#Http to https
#Exclude subdomains
RewriteCond %{HTTP_HOST} !^(sub1|sub2)
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
#add www on ssl
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
This will redirect :
http://example.com
or
http://www.example.com
to ssl
https://www.example.com
And the second rule will add www to non www requests on ssl, redirect :
https://example.com
to
https://www.example.com

First, you can edit your httpd.conf file, and add this to the :80 servers
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [L,R=301]
</IfModule>
Anyway , this on .htaccess will do the same
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R,NE]
Also, you can add in your vhost file also something like
Redirect permanent / https://www.domain.com/
And in your vhost_ssl:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [L,R=301]
</IfModule>

Related

redirect all non-www to www, all http to https, and exclude subdomains

I am trying to redirect
example.com to www.example.com
and
http://example.come to https://www.example.com
HOWEVER, if a user types in
admin.example.com, http://admin.example.com or https://admin.example.com
it should NOT redirect to www.admin.example.com
What .htaccess can I use for this?
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Regex pattern (?:www\.)?([^.]+\.[^.]+) will match example.com or www.example.com but not admin.example.com

Force www. even with subdomain not working without /

I have a site example.com and also a subdomain and I want the following redirect:
subdomain.example.com -> www.subdomain.example.com
example.com -> www.example.com
I use the following code
RewriteEngine On
# Force www. always and SSL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
# Force SSL if already www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
Now the problem is that subdomain.example.com doesn't redirect but subdomain.example.com/ does.
I also get a Server Error when going to example.com, although it does redirect to www. correctly.
The point with these redirects is so that I can in the same .htaccess file in the root folder hopefully redirect the subdomains to subdomain.com more easily, I have several domains on one server.
I think you can combine the directives together like this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
=> Check if HTTPS is off, or if the domain name does not begin with www, and then redirect to https://www.whatever.

Redirect www and http to non-www with https using .htaccess

I've got the following written into my .htaccess file which is placed in the directory where my index.html is.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URL} [L,R=301]
I want that
www.example.com
http://example.com
and http://www.example.com
are redirected to
https://example.com
Do I have to exchange REQUEST_URI with example.com or am I doing something else wrong ?
You must put an OR in between your rewrite conditions
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Note: You have to test the urls as:
http://www.example.com
http://example.com
https://www.example.com
They will all be redirected to
https://example.com

HTTPS for www and non-www only - HTTP for all subdomains

I have certificate for single domain only, including www.
With my current .htaccess setup, I am redirecting all HTTP requests to HTTPS.
But I want to make it following:
Force HTTPS domain.com (www and non-www)
Allow HTTP only for ALL subdomains *.domain.com
Is this possible?
My current .htaccess setup forces everything to use HTTPS
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have found 1 solution:
# Force HTTPS www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# Force HTTP subdomains
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

htaccess redirect HTTPS to secure.domain.com, non https to www.domain.com

Not sure how to best write the following rules:
All secure (HTTPS) traffic should redirect to https://secure.domain.com
Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
All other non-secure requests should go to http://www.domain.com
Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
Try adding the following to your htaccess file in the root folder of your domain.
RewriteEngine on
RewriteBase /
#Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.domain\.com$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#All secure (HTTPS) traffic should redirect to https://secure.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.domain\.com$ [NC]
RewriteRule .* https://secure.domain.com%{REQUEST_URI} [L,R=301]
#All other non-secure requests should go to http://www.domain.com
#Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .* http://www.domain.com%{REQUEST_URI} [L,R=301]