.htaccess how to handle dynamic sub-domain redirect to https and no sub-domain to www - apache

I have my htaccess file gitignored and while upgrading to Laravel 5.2 I deleted the old one on the server. I spent hours piecing it together and now I'm struggling to recreate it.
I need anything like
http://domain.com
or
https://domain.com
to goto
https://www.domain.com
and all other wildcard sub-domains such as:
http://myname.domain.com
to goto
https://myname.domain.com
So far I have this:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

To redirect subdomains and main domain to HTTPS, you can use:
RewriteEngine on
#--Redirect subdomain to https--#
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]
#--Redirect maindomain to https --#
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NC,R,L]
#--Redirect non-www to www on SSL--#
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTPS} on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
Change R to R=301 when you are sure the redirect is working fine.

Related

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

http to https redirect along with www to non-www in htaccess

I'm trying to redirect my site from http to https and from www to non-www at the same time. This code from Andron here works but with a few glitches. I've slightly tweaked it and redirected everything from the non-https to https as well.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
For the following situations the redirect works flawlessly and redirects to the non-www https version:
domain.com
www.domain.com
www.domain.com/page
However, when I enter the domain.com/page or http://domain.com/page I see only the non-secure http non-www version.
How do I make sure that all URLs redirect to the secure https non-www version of the site?
Here is a rule that does both http->https and www removal in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Multiple redirects should better be avoided for SEO purpose.
You can use:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

redirecting https://domain.com to https://www.domain.com

I've recently moved my site to HTTPS and im trying to force all http traffic to https and also to the www. version
i.e forcing
https://domain.com
to
https://www.domain.com
I have this in my htaccess and all http to https works, apart from the redirect from https://domain.com to https://www.domain.com
What am i doing wrong?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need an OR condition here:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
with the above rule https://example.com was not redirecting to https://www.example.com.
For that to happen I have added the following rule too in the apache ssl vhost block
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exmple.com%{REQUEST_URI} [R=301,L,NE]

Ignore No Subdomain In htaccess RewriteCond

I've got htaccess set up that redirects all subdomains except www to https. However I'd also like it to ignore http://domain.com - currently it redirects to https://domain.com which is no good for what I'm doing.
Any idea what the rewrite condition is for that?
I've tried:
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteCond %{HTTP_HOST} !^http://domain.com [NC]
But neither of those work.
Here's what I'm using at the minute.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I think your problem is that you need to escape the dot in domain.com, so domain\.com.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You used a 301 (permanent) and not a 302, so you may have your browser not even trying to send requests to the http domain until you close. You should use 302 while testing and put the 301 only wen it's ok.
Try this:
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]