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

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]

Related

www to non-www redirect in my site not working?

This questions has been asked many times on this site and I have tried most of them but nothing worked for me.
I have a site which works fine at http://example.com/ but when I apply rewritecond & rewriterule to redirect from www( http://www.example.com/ ) to non-www( http://example.com/ ), it's not working as expected.
I have tried the following solutions
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ http://example.com/$1 [r=301,nc]
it works fine at http://example.com/ but show the following error at http://www.example.com/
server IP address could not be found. DNS_PROBE_FINISHED_NXDOMAIN
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(.+)
RewriteRule .* http://%1/$0 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* http://%1/$0 [R,L]
It shows the same error as above.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP} off
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,R=301,L]
This shows the error
To many redirects
Error message says it all. Please add A record for www.example.com(Same IP as example.com) as well in DNS control panel.

Can't redirect to https (htaccess)

I was trying to configure my htaccess file which will redirect from http to https with following lines of statement. But it couldn't .
Note: i don't want the www before the site URL as well.
What could be possible error of my code. Anyone please suggest.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]
Problem appears to be this line:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
This line makes this rule work for domain with starting www only.
You need to make starting www optional by using:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
As another solution, if you have only one domain name (www.)example.com, you can use more readable:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Also, you can also redirect based on port number, for example:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This will redirect all requests received on port 80 to HTTPS.

Forcing https on the whole site but not on one folder

Could anyone tell me how I can force https on my whole website but not on a single folder or url.
At the moment I have this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]
But if I add this second code in the root htaccess to remove https from the /thatsite.com/printing folder, I get a redirect loop because I am forcing on the code http to https and not https to http...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Do you know a way around this please? I have been looking all over the internet and cannot find a single good answer.
Try these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I solved this issue by placing this code in an .htaccess file the root folder of the directory that hosts the http site (e.g. public_html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]

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]