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

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

Related

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.

how to make htaccess force https for www.example.com and example.com but no other subdomains

I have a host that has many subdomains and I want to force https (redirect for https ) for www.example.com and example.com but not any other subdomain, how to do this?
currently I am using :
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
but this does not work as expected
You can just add a condition using HTTP_HOST:
RewriteCond %{HTTPS} !=on
RewriteCond HTTP_HOST ^(?:www\.)?example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

How to redirect www to non-www https?

I have installed https on my domain. And all this cases are available of my website:
http://example.com // will be redirected to https://example.com
https://example.com
http://www.example.com // will be redirected to https://www.example.com
https://www.example.com
See? everything will be using https protocol. All fine.
Now I need to redirect all www to non-www. So http://www.example.com and https://www.example.com should be redirected to https://example.com (in other word, all cases should be redirected to this).
Here is my current code in the /etc/apache2/sites-avalible/000-default.conf:
<VirtualHost *:80>
.
.
.
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com [OR]
RewriteCond %{SERVER_NAME} =www.lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] -- and this are for www redirection
-- but doesn't work
</VirtualHost>
any idea?
also lamtakam is my exact domain I was talking about, if you need to check something.
Edit:
Currently I have this inside .htaccess file on the root of my project:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# this doesn't work on some version of xampp
# RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
ErrorDocument 404 /error404.html
If you want rules in VirtualHost then it would be 2 set of rules. If you can keep rules in site root .htaccess then it would be a single rule. I am providing .htaccess rule here:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure you remove your existing rules shown in VirtualHost above and you test in a new browser to avoid old browser cache.

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

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

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>