.htaccess redirect excluding sub-domains - .htaccess

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.

Related

.htaccess redirects - map all URLs but give homepage specific redirect

I have the following set up for an old domain, which works really well to mapping the old pages to the ones on the new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldurl.org.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org.uk [NC]
RewriteRule ^(.*)$ http://www.newurl.co.uk/$1 [L,R=301,NC]
The problem is I need to redirect the homepage to a specific page on the new URL, keeping the above in tact. Whats the best way to do this?
Thanks in advance!!
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^$ http://www.newurl.co.uk/specificPage [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^(.+)$ http://www.newurl.co.uk/$1 [L,R=301]

Swapping domain name with htaccess

I'm trying to migrate a website to another domain and hope to have requests on the old domain redirected to the new domain, no matter what they are, and regardless of how the URL is structured.
The code below in my .htaccess file does this for a domain and any directories/pages but does not cover the many subdomains I have.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
How can I add to this in order to convert the following (for example):
http://something.domain1.com/directory/page.php?variable=something
http://something.domain2.com/directory/page.php?variable=something
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^(.*)$ http://domain2\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://%1.domain2\.com/$1 [L,R=301]

Pointing multiple domains to specific pages via htaccess

Let's say may main domain is www.mysite.com and that I'm using the following rewrite in my hataccess
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
All good so far! However, I also need other domains to redirect to specific pages on the site. For example, if somebody types www.mysite.nl they will be redirected to www.mysite.com/neatherlands. The issue is when I pop the below into my htaccess file I get a server error. I think this is because of the final rewrite. It's a pretty mad set-up
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.nl$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/netherlands/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.be$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/belgium/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.\uk$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
Any suggestions about what to do?

Redirect variable page to variable subdomain wildcard

I am stuck on the follow htaccess situation.
I want to redirect pages to subdomains.
Url example right now;
http://domain.tld/user/foo
I need to redirect this to;
http://foo.domain.tld
The subdomain is genereated by wildcard.
I tried the follow without succes;
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteRule ^user/(.*) (.*) [R=301,L]
Regards,
Nick
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld [NC]
RewriteRule ^user/([a-zA-Z0-9-.]+) http://$1.domain.tld/ [R=301,L]

Redirect from old site and subsites to new domain - main page with htaccess

I have the following rules in my .htaccess
rewritecond %{http_host} ^oldname.com [nc]
RewriteRule ^(.*)$ http://www.newname.com [r=301,nc]
rewritecond %{http_host} ^www.oldname.com [nc]
rewriterule ^(.*)$ http://www.newname.com [r=301,nc]
I am wondering how to merge does 2 rules in one. Beside that I would like to redirect if user comes to: www.oldname.com/?action=whatever to newname as well...
Thanks for response!
rewritecond %{http_host} ^(www\.)?oldname.com [nc]
RewriteRule ^(.*)$ http://www.newname.com [r=301,nc]
change the above code to that
rewritecond %{http_host} ^(www\.)?oldname.com [nc]
RewriteRule ^(.*)$ http://www.newname.com/$1 [r=301,nc]
If that not work tell me to give you another solution.

Resources