Pointing multiple domains to specific pages via htaccess - .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?

Related

.htaccess redirect specific domain ending to folder

i have several domains (same name, but different endings) for the different languages. the .com domain is the main domain and has for each language a directory like /en/
now i want to redirect each of these domains ( e.g. http://example.us/ ) to http://example.com/en/
is this possible with the .htaccess file?
actual i only have a redirection from www.example.com to http://example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
You can do it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.(at|ch)$ [NC]
RewriteRule ^(.*)$ http://example.com/de/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.us$ [NC]
RewriteRule ^(.*)$ http://example.com/en/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.fr$ [NC]
RewriteRule ^(.*)$ http://example.com/fr/$1 [L,R=301]
But if you have more domains (maybe a lot) you could use RewriteMap

htaccess redirect to domain without subdomain with no hard coding

i need to redirect all requests of form http://static.name.ext/first/second/etc
to http://www.name.ext if first segment is not "assets"
so keep requests of form http://static.name.ext/assets/etc redirect others
i tried with
RewriteCond %{HTTP_HOST} ^static\. [NC]
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [R=301,L]
but %{HTTP_HOST} returns the subdomain and i don't want that
Try:
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteCond %{HTTP_HOST} ^static\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/ [R=301,L]

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 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.

Joomla www enforcing in urls with .htaccess, not working as it should

I have a joomla 1.6 website, and I want to enforce a www in the beginning of urls.
I've written the following into htaccess:
RewriteCond %{http_host} ^example\.com [nc]
RewriteRule (.*) http://www.example.com/$1 [r=301,nc]
What it should do is redirect from example.com/happy_bunnies.html to www.example.com/happy_bunnies.html. Instead it redirects to www.example.com/index.php
RewriteCond %{http_host} ^example\.com [nc]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule .* http://www.example.com%1 [r=301,nc,l]
No idea why the first didn't work, but this one does
Have you tried...
RewriteCond %{http_host} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

Resources