Add www to domain but not for an specific domain - .htaccess

I've the following code in my .htaccess file:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The code above is for add www in the domain if it does not have the www, but I've a domain like: myloadbalancername-432566808.us-west-1.elb.amazonaws.com (DNS from AWS Elastic Load Balancer) and this domain does not work with www, so, how can I do to add www for all domain requests, except for the specific domain?
Update:
RewriteCond %{REQUEST_URI} !myloadbalancername
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I tried with the code above, to check if domain name does not have the word, so, if not have, check if domain does not have the www but without success. I'm beginner with .htaccess so I don't know what I'm doing wrong.
Thanks in advance.

Try with:
RewriteCond %{HTTP_HOST} !myloadbalancername
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Because host name is not part of %{REQUEST_URI}

Related

.httaccess redirect from old domain to new domain into a directory

i have to domains which DNS shows on the same server. Now I would like to make a htaccess-redirect from one Domain (old domain) to a directory on the new domain
exampl:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.ch$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.ch$
RewriteRule (.*)$ https://newdomain.ch/de/besuchen/$1 [R=301,L]
I put these file on the root of the newdomain.ch but if I open olddomain.ch it only shows me the homepage and not the side /de/besuchen
thanks for help.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Usage: These rules will redirect all traffic from olddomain.com to newdomain.com.

Sub-domain Getting Issue with .htaccess

I have a domain "example.com" and i use following redirection code to redirect it to www in .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
it is working fine until i generate a sub-domain with that domain like "abc.example.com" but it's getting conflicts with htaccess and redirecting the sub-domain to "www.abc.example.com/abc/"
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|abc)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect subdomain to main domain except one url

Hi I have a main domain as follows
Main domain : http://www.sample.com/
Sub domain : http://dev.sample.com/
Now If any one access http://dev.sample.com/ It needs to redirect to http://www.sample.com/
So I added the following code in my sub domain It works very well
RewriteCond %{HTTP_HOST} !www.sample.com.com$ [NC]
RewriteRule ^(.*)$ http://www.sample.com.com/$1 [L,R=301]
But from subdomain if any access the following url http://dev.sample.com/data then
it should not redirect to main domain. It should stay on that sub domain page. Any one have idea?
Try these rules :
RewriteCond %{HTTP_HOST} ^dev\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?data
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,QSA,R=301]
Try this:
RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/data [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
However, this will prevent all URL's with /data at the start of the URI. So if you had http://blog.sample.com/data it would also allow it to resolve without the rewrite. If this is a problem you can get more complex... after this rule add this to catch all non-dev subdomains:
RewriteCond %{HTTP_HOST} !dev\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
Now if the first condition set is not met the first rule won't run, so check to make sure you're only serving dev and www up with /data.

.htaccess redirect excluding sub-domains

I have the following in my .htaccess to redirect all old domain names which point to the same location to the new domain, but this doesn't allow any sub domains of the new domain name to work - any idea how to amend the code to allow sub-domains would be appreciated:
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
Redirect subdomain to subdomain under a new domain...
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [R=301,L]
Thanks Brian - it certainly helped - the final outcome is the following:
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC] [AND]
RewriteCond %{HTTP_HOST} !^(.*)\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
Thanks for your time.

Htaccess to force use WWW for mutlidomain

I found this code to force using www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
But i have many domain that pointed to same directory. So i need a version of this code for multiple domains. It must run on any domain, is it possible ?
This should work:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
It will redirect all request without a subdomain to www.domainame.tld.

Resources