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

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]

Related

htaccess rewrite rules - domain/subdomain https/www

I have my main domain and a subdomain. Subdomain is located in a subdirectory on the root directory of main domain and this cannot be changed. Main domain has an SSL while subdomain doesn't.
Now I am trying to force https and www for the main domain and force no https and no www for the subdomain. So at the end the main domain disregarding how it will try to be accessed should redirect at https://www.example.com and same time subdomain should be http://sub.example.com
All other possible combinations of accessing them should redirect to the above 2. So far, I am successfull to do this for the most possibilities but I am failing to do so for the case when I access the subdomain with https://sub.example.com. It won't redirect to the http version, rather it will try to load the page through https, browser throws the security warning about the SSL and if I proceed, it load the contents of the main domain site through the subdomain and the insecure warning on the browser.
Following is the htaccess rules I am trying, on the htaccess on the root directory, that affects both main site and subdomain.
How to improve them and achieve the desired outcome?
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\.example\.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !sub\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www\.sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
To redirect only the main domain to https, you can use the following rule :
RewriteEngine on
#https to http (subdomain)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [NE,L,R]
#main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

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

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.

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]

Redirect http://www.domain.com to https://www.domain.com using .htaccess

I want to redirect my domain from http to https:
http://www.domain.com --> https://www.domain.com
Is it possible? I've been searching around the internet but I only found:
http://domain.com --> https://www.domain.com
The question is, how about peoples arriving directly on http://www.domain.com? Aren't they be served with non-https url? Same as the vice versa. I just want a simple redirection from the HTTP to HTTPS. Is it possible?
Thank you
I just want a simple redirection from the HTTP to HTTPS
Try this simple rule as first rule on your .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
It will work for ALL versions of php and will force SSL and www to all naked domains and links:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
We DO need the NE
www. is optional in 4th line
"off" is required to be.
Use the following code for force www and SSL usage:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^tatwerat\.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

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]