Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Is it possible to configure a top-domain ( (DNS/.htaccess/else?) to redirect to another domain (, whilst if you navigate, you'd see the files in the subdirectory of

how to: -> (redirect) -> (no redirect)

Case use
I've got and, which are both my business names. One is my own personal name, the other my brand name. When a user navigates to, the domain should be forward (301) to However, when one types (i.e.) it would stay on that spot. So only the top-level gets a 301 redirect, but typing in the top-domain including a subdirectory will result in NO redirect.

For one saying 'Why don't you use the test sites in a subdirectory on the main site ('; that's because I'm testing (temporarily) out a new server, where currently is hosted on. :)

share|improve this question
It is possible. What part are you stuck on? – JamesRyan May 12 '14 at 17:12
The start. :) - Wondering what is recommended: Using HTaccess or DNS and what lines are needed in either the DNS or htaccess file. :) – Sander Schaeffer May 12 '14 at 17:18
Just you just clarify... From the examples you have given above, you are referring to a "subdirectory", not a "subdomain"? – w3d May 12 '14 at 23:50
Domain should redirect to the new domain, but if one types in the domain + subdirectory (subdirectory, not domain, indeed) then it would stay on the top domain. So goes to, but remains on – Sander Schaeffer May 13 '14 at 8:39

1 Answer 1

up vote 3 down vote accepted

Assuming you mean "subdirectory" and not "subdomain" (as per your examples), then try something like the following in the .htaccess file at

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule (.*)$1 [R=301,L]

Redirect (301) everything to where the URI does not start with /sub/.

Note that this redirects to the corresponding URL at, so for instance, redirects to (Rather than literally only redirecting to

UPDATE#1: In light of the comments below, the following might be sufficient. This redirects (and only the root URL) to Anything else eg. stays at

RewriteEngine On
RewriteRule ^$ [R=301,L]

Note, however, that this does not specifically check for a valid subdirectory. Any path component in the URL will result in the URL staying at

If you specifically need to check for valid/known subdirectories then I would use a RewriteCond directive as in the first example above.

UPDATE#2: Another possibility is to check for anything that looks like a subdirectory ie. /anything/ and redirect otherwise. For example:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/[a-z0-9_-]+/
RewriteRule (.*)$1 [R=301,L]

This will redirect to and to, but will not redirect

share|improve this answer
As I just added in the comments in the OP, when a user types in, then it would go to However, when an user adds a subdirectory to the URL, i.e., then it would stay on I've added a case to the OP to explain the situation, Thanks. – Sander Schaeffer May 13 '14 at 8:40
That's what I assumed - the code I added above should do that. However, when you say "subdirectory", do you mean any subdirectory ie. "/sub1/", "/sub2/", etc. Although if there are only a few then it would be easier to just add an additional RewriteCond for each subdirectory. Also, I assume and are separate hosting accounts? – w3d May 13 '14 at 9:47 ->; -> - one method for all subdirectories of And yes, and are separated hosting accounts, right now. :) – Sander Schaeffer May 13 '14 at 10:12
I've updated my answer with a couple more examples. I can't just think of a way to check for a valid subdirectory in .htaccess at the moment, but maybe one of these examples will work for you? – w3d May 13 '14 at 14:10
I think 'update #2' will perfectly fit. As,, and will get redirected to incl. respected files (as in example further), while anything within a subdirectory stays on e.g. goes to, while stays there. If that's indeed the case, please reply and verify or clarify yourself. Than I'll mark this answer as accepted and the issue has been solved! =) – Sander Schaeffer May 13 '14 at 18:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.