Forward non-www to www except for specific subdomain - .htaccess

I am routing all non-www requests to the www-domain by using this RewriteCond and RewriteRule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But there is one certain subdomain that I want to exclude, e.g. blog.mydomain.tld. How do I have to modify my condition and rule in order to achieve this?

You can just add that subdomain in regex of your RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|blog)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]

Did you try adding
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^blog\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]

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.

Htaccess rewrite rule and condition

We are now using OpenCart and domain aliasses in DirectAdmin.
We now have some rules in our htaccess for redirecting non www to www and non https to https.
But this rule is for all domains. We only want to use it for example-1.com
How can we do this? Htaccess we have tried all failled to do this.
This is the current htaccess we have
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
We only want this for example-1 and example 2 also. But now it always redirects to example-1
I understand you only try to redirect example-1 and example-2 and not other domains.
You can use that:
RewriteCond %{HTTP_HOST} (?:^|\.)(example-1\.com|example-2\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]

Redirecting specific Domains to HTTPS

My domain www.abc.com is now redirecting to https://www.abc.co.uk
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]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]

htaccess redicect to www exclude subdomain

i have the following rule to redirect non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
but now i want a subdomain assets.company.com το be excluded but nothing seems to work for me
i have try
RewriteCond %{HTTP_HOST} !^assets\. [NC] [AND]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
and its not working
Pleas help
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|assets)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Make sure to keep this as first rule.
Make sure to test this in a new browser to avoid caching issues.

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.

Resources