In .htaccess, redirect all domains except one - .htaccess

I have multiple domains on my server. I want to redirect all of them to one (example.net).
My .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
I’m redirecting all URLs on my server to one main domain, but that domain is also redirecting to itself. So www.example.net returns 301 Moved Permanently and redirects back to itself. I’m told that this isn’t good for SEO. How could I fix this?

You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.
What you’re looking for is something like:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

Just small note:
Thanks goes to TRiG, but I had to remove one slash to make it work correctly (coz it added two slashes after domain name). This works for me:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net$1 [R=301,L]

Related

Only top-level domain redirect using .htaccess

I have some htaccess that looks like:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?joshrodg\.com$ [NC]
RewriteRule ^ https://joshrodgers.com/$1 [R=301,L,NE]
The first three lines are forcing all of my sites to be https, so we're all good there. The last two lines, I would like to take the non-www and the www of joshrodg.com and redirect to https://joshrodgers.com/.
My code is working, the thing is, it's working a little too good. I use other paths from joshrodg.com like joshrodg.com/example or joshrodg.com/test but I don't want these paths to redirect. I just want the top-level domain to redirect. I'm thinking this is just a quick tweak and I've tried several pieces of code, but am still getting redirected.
Any ideas?
Thanks,
Josh
With "just the top level domain" you actually mean requests to the root folder or to the path "/" only. So you need to match exactly that path only:
RewriteRule ^/?$ https://joshrodgers.com/ [R=301,L]

.htaccess write rule for subdomain to subfolder

I would like to rewrite requests to a subdomain to a sub-folder on the site in question.
The subdomain is dev.my-domain.co.za (yes, it has a hyphen in it)
The folder it needs to internally reference is my-domain.co.za/dev.my-domain.co.za
I don't want it to redirect, but to rewrite, so that the user just sees http://dev.my-domain.co.za/...
I found various examples here on StackOverflow, but I was unable to get them to work in my scenario. So I figure it's best I ask and explain my exact situation.
There is going to be a Wordpress site in the sub-folder. I am assuming it is okay to call it via a sub-domain (if you happen to know, please advise). Otherwise, if there are issues, I can ask that question separately in a WP related forum.
So far I tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^dev\.domesticflights\-southafrica\.co\.za.*$
RewriteRule (.*) dev\.domesticflights\-southafrica\.co\.za/$1 [L]
Which is my attempt to apply this (from here) to my scenario
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.example\.com.*$
RewriteRule (.*) myfolder/$1 [L]
I tried adapting many other examples, but none work in my scenario.
You can try these rules to internally redirect from
dev.my-domain.co.za to my-domain.co.za/dev.my-domain.co.za
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www)(.+)\.(my-domain\.co\.za)$
RewriteRule ^ http://%2/%1.%2 [P]

RewriteRule to redirect subfolder and parameters to parked domain

This won't work, I don't know why...
# let's say I'm in HTACCESS of domain www.aaa.com
RewriteRule subfolder/script\.php\?param=value&param2=value2 http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L]
# domain bbb.com is mine, BUT not on the same account (but same server)
It's not causing any bug or loop, it simply plain not work.
Other instructions in the HTACCESS does work (rewriterules among them) so I know the RewriteEngine is on and works.
It simply won't work because RewriteRule doesn't match QUERY_STRING. You will RewriteCond for that. Use code like below:
RewriteCond %{QUERY_STRING} param=value&param2=value2 [NC]
RewriteRule ^subfolder/script\.php$ http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L,R]

.htaccess rewrite add sub-domain exception (remove redirect)

I have checked previous questions, I believe this is quite simple but I can't seem to work it out.
In my .htaccess file I currently have
RewriteCond %{HTTP_HOST} !^www\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
However this is causing a problem as it also redirecting any sub-domains (specifically au.bodycleansediet.com and ca.bodycleansediet.com) to www.bodycleansediet.com
I want them NOT be to be redirected so they can be viewed on their sub-domains.
I know I need to add an exception/re-write rule but I am not sure how to construct it.
Any advice on how to construct this?
Should not something along the following lines work
RewriteCond %{HTTP_HOST} !^(www|ca|au)\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
Since you are basically checking if not (www OR ca OR au) then do your redirect.

Problem setting 301 redirection statements in htaccess

I'm having some trouble setting up a htaccess file. Currently I have 15 domains serving their own website. Now moving this to a single website and domain, I want to serve a htaccess with 301 rules for the old urls. The destination url depends on the domainname of the old url..
E.g.
http://www.previouswebsiteinspanish.com/contacto should be permanently rewritten to http://www.newcentralwebsite.eu/es/contact
I understand that I have to use the RewriteCond but I'm not familiar enough with it to get it working.
RewriteCond %{HTTP_REFERER} !^http://(www\.)?previouswebsiteinspanish/.*$ [NC]
RewriteRule ^([^/.]+)/contacto$ /$1/contact [R=301,L]
Thanks in advance!
You current condition is that the rule will be executed under the condition that the referring page is not on the old domain. That's most likely not what you want to do. :)
You want to check the %{HTTP_HOST} parameter, which contains only the hostname part of the currently requested URL.
Assuming you want to redirect everything on the old domain to the es subfolder, this rule will do it.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish.com$ [NC]
RewriteRule ^(.*)$ http://www.newcentralwebsite.eu/es/$1 [R=301,L]
Then you'll have to add similar rules for each old domain.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
You need to check HTTP_HOST which is the hostname of the requested site.
From your example the rule I wrote is
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
Add such rules to your .htaccess.
You have to write such rules for each domain.
In above case you have change in url too, so you may have to handle such special cases too.

Resources