Reditect main domain to https, but not subdomain - .htaccess

I have a website say example.com and a SSL for www.example.com.
I want - if anyone goes to example.com or www.example.com, he is redirected to https://www.example.com/.
But if he goes to sub.example.com, he should not be redirected to https, i.e. stay on to http.
I've gone through various answers on StackOverflow, but could not configure it out. Currently, I'm using the following:
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://www.example.com/$1 [R,L]
This redirects sub.example.com to https://www.example.com/sub/, which I don't desire to.
Please help!

You want to add an extra condition that checks the current hostname. You can do this with the %{HTTP_HOST} variable.
RewriteCond %{HTTPS} !^on$
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) https://www.example.com/$1 [R,L]

Related

Redirect to Https exclude subdomains but regardless of WWW or not

I haven't seen this specific question on here but I'd like to know how to redirect the main site I have, regardless of whether someone includes "www" in the URL, but not redirect any subdomains. How do I do it?
This is what I have but it only works if they add the "www".
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
You may use this rule to redirect main domain with or without www:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
^(?:www\.)? matches optional www at the start.

Dynamic redirecting all subdomains with htaccess

I've got the following code that will redirect "www.domain.com" to "domain.com". This is fine but I want it to redirect all subdomains, ie. "test.domain.com" to "domain.com" as well. It has to be dynamic, meaning that the domain name can't be hardcoded.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I've tried various stuff but I can't get it to work.
This will do it:
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[^.]+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It supports anything.example.com and redirects to example.com and example.com can be served on the same site using the redirect.

Redirect domain to WWW

I want to make a URL change whenever clients access my website with example.com to wwww.example.com I have used .htaccess for this but it works not correct.
Because now a-z0-9.example.com wil also redirect to www.example.com, I want this only works for example.com than redirect to www.example.com
.HTACCESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.eample.com/$1 [L,R=301,NC]
Make another .htaccess for a-z0-9.example.com with the code bellow:
RewriteEngine on
RewriteCond %{HTTP_HOST} a-z0-9.example.com [NC]
RewriteRule ^(.*)$ http://www.a-z0-9.example.com/$1 [L,R=301,NC]
I think this will work.
Good Luck!

Force all requests to use https:// and prepend www

I have spent countless hours trying to solve this problem and it's doing my head in. I'm sure that it is very simple but none of the answers I have found either on stackoverflow.com or other sites work for me.
I am using an SSL certificate that is only valid for www.example.com and for a myriad of silly corporate reasons I am stuck with only this certificate.
I would like to force all requests to use SSL and prepend www. to the domain. All of the following domains rewrite correctly:
example.com/
http://example.com/
http://www.example.com/
https://www.example.com/
The domain that is not rewritten and is the one problem I cannot find the answer to anywhere is
https://example.com
It simply spits out the usual "Certificate Is Not Valid" warning.
I have the following code in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
I really hope that somebody has a solution for me because I cannot find the answer anywhere.
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=302,L]
It will check to see if it has www in the url, if it doesn't then it will add it. Then it will check and see if it is https and if it isn't then it will add it.

htaccess redirect to HTTPS

I've been trying to figure out a way to redirect only one domain to HTTPS but haven't found a good solution. I was able to redirect all requests to https by using the HTTPS!=on condition but I host multiple domains and only one has SSL.
This gave me some success.
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But it doesn't seem to redirect URLs like www.mydomain.com/order/ and mydomain.com/order/
Basically it works only for the main page currently at www.mydomain.com or mydomain.com.
What am I missing?
Your problem is this:
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
That says, "Only use the next RewriteRule if the host is 127.0.0.1 or localhost, exactly." If you host mydomain.com on that same server, it won't match. You need to add the name of the domain you want to redirect. Example:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources