htaccess redirect all to www except IP - .htaccess

All my website page URL's are redirected to have a www at the beginning. So, if someone types http//xyz.com it will automatically make it http//www.xyz.com. This is fine for me. But the main problem is, if someone types 123.234.111.121 it will again put a www at the beginning, which is wrong. How do I fix it? Any help in this regard is highly appreciated.
My current setup is as follows
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I even tried with this...
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^50\.56\.246\.162
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
But, it still doesn't work

You can use this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^[0-9]+\.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This condition RewriteCond %{HTTP_HOST} !^[0-9]+\. will skip this rule if you supply an IP address to access the website.

The above rule as suggested by anubhava is not working for me also. I google around and came with this answer.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Redirect IP to Domain Name in AWS [duplicate]

Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
What am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Alter "111" to your IP
Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]
Hope it helps and works for ya'll. Thanks for the thoughts.

Redirect non-www to www using .htaccess

I access a file on my root like example.com/demo/provider it is redirecting to www.example.com.
How can I correct this so that it will redirect correctly to www.example.com/demo/provider
What I have tried:
Try 1
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Try 2
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Try 3
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I have tried some other codes too, Any suggestions will be appreciated Thanks!
Note: I already searched options available on Stackoverflow. But, none of answer worked for me. So, its not a duplicate question. I respect rules and terms of site.
My .htaccess file is in provider folder. You can take it as sub domain/directory.
If your htaccess is in the /provider folder then you have to use the full path to dir in target url,
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/demo/provider/$1 [R=301,L]
Or
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This will correctly redirect your url from :
http://example.com/dir/foobar
to
http://www.example.com/dir/foobar

redirect domains and subfolder to new domain htaccess

I have 4 domain which I want 3 of them redirect to last one. I already used some htaccess rules and they are working great,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.com$ [NC]
RewriteRule ^(.*) http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.first.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
the only problem is when I enter "www.first.com/about" it does not shows "www.forth.com/about" just shows the same thing . all the domain are forwarded to forth.com and they do not have hosting so I cannot put htaccess file for those other domains , please guide. would be appreciated.
This is most likely what is happening, at least on my test cases this fix handles it.
Instead of using [L,R=301] reverse the order and do [R=301,L]. The L means Last and it ends cycle on evaluating what to do at that point. I've always used [R=301,L] as are all the examples that I could find anywhere. Potentially your apache may be hitting that L and immediately pass back the new string to the URL parser and isn't sending it as a 301 Redirect as it didn't get that far.
Also on the first.com snippit above you left out the $ on the ^(.*) - only for that line though, not sure if this was a typo or if it's missing in your actual .htaccess file.
Otherwise, you could tidy it up a bit by using:
RewriteCond %{HTTP_HOST} ^((www.)?)first.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)second.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [R=301,L]

Redirect IP to domain

Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
What am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Alter "111" to your IP
Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]
Hope it helps and works for ya'll. Thanks for the thoughts.

Redirect the non-www URL to the www URL

I have referred all the related questions and tried the answer given, but it's not working for my site.
In my .htaccess file, I have written below code to redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Are there any settings in Joomla that need to be set so it will use the .htaccess file?
This is what I use -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The differences are minor but should make it work. This works completely outside the scope of Joomla, you shouldn't have to do anything to Joomla for this to work.
You can put the following into your .htaccess to do this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
this works for me all the time

Resources