Ignore No Subdomain In htaccess RewriteCond - apache

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]

Related

redirect www and non-www to https www .htaccess not working

Please don't say, its duplicate I have been through all threads but none of them worked for me.
I have installed SSL and want to redirect www and non-www to https://www
I have this code .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ven7\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.ven7.com/$1 [R,L]
This is working fine, redirecting domain.com to https://www.ven7.com
But if I hit www.ven7.com, page is loading fine but its taking http, so I have changed script to..
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ven7\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.ven7\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.ven7.com/$1 [R,L]
With this code redirection is working but page is not loading, giving some error, too many re-directions.
How can i fix this? and want this redirection permanently.
Thanks
You can use:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.ven7.com%{REQUEST_URI} [NE,R=301,L]
If you need to test the domain name, add first line:
RewriteCond %{HTTP_HOST} (?:^|\.)ven7\.com$ [NC]
And use:
RewriteEngine On
RewriteCond %{HTTP_HOST} (?:^|\.)ven7\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.ven7.com%{REQUEST_URI} [NE,R=301,L]

.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.

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]

Redirect https and http www to https non www

I want to rewrite the following
http://example.com
http://www.example.com
https://www.example.com
to
https://example.com
tried using
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It doesn't seems to be working
I'd say the easiest thing to do is just write two rules:
# First make sure HTTPS is being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then make sure www isn't being used
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.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>