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

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.

Related

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.

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: rewrite all requests to https and domain to subfolder without listing subfolder

I want to achieve the following below.
Rewrite (www.example.com, example.com) to (https://example.com/folder) without showing /folder in the url i.e (https://example.com)
Also want other requests to (www.example.com/other-folders/..., example.com/other-folders/...) to rewrite to https i.e (https://example.com/other-folders/...)
At the moment, I have this below:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*)$ /folder/$1
www.example.com - Works perfectly
example.com - Works but returns this error "incorrect access detected, this server may be access only through https://example.com/folder... then redirects but shows /folder
www.example.com/other-folders and example.com/other-folders - Doesn't work
Try :
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule (.*)$ /folder/$1

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]

Force SSL on your website using .htaccess and Forcing the "www." at the beginning of URLs

I was using this code and it successfully forces ssl:
RewriteEngine On
RewriteCond %{HTTP_HOST} www\.example\.com
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
however it does not force www at the begining of urls. I got this supposed solution posted on another page here:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
But what i get when i attempt to open my page is this:"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
How may i fix this, that is, not only force ssl but also force www at the beginning of urls?
Thanks.
Try:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]