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

how to:
x.com -> y.com (redirect)
x.com/sub/index.html -> x.com/sub/index.html (no redirect)

Case use
I've got sanderschaeffer.nl and iscs.nl, which are both my business names. One is my own personal name, the other my brand name. When a user navigates to sanderschaeffer.nl, the domain should be forward (301) to iscs.nl. However, when one types (i.e.) sanderschaeffer.nl/clientTestSite/ 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 (iscs.nl/)?'; that's because I'm testing (temporarily) out a new server, where sanderschaeffer.nl currently is hosted on. :)

  • It is possible. What part are you stuck on?
    – JamesRyan
    May 12, 2014 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. :) May 12, 2014 at 17:18
  • Just you just clarify... From the examples you have given above, you are referring to a "subdirectory", not a "subdomain"?
    – MrWhite
    May 12, 2014 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 X.com goes to Y.com, but X.com/sub remains on X.com/sub. May 13, 2014 at 8:39

1 Answer 1


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

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

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

Note that this redirects to the corresponding URL at y.com, so for instance, http://x.com/foo/bar redirects to http://y.com/foo/bar. (Rather than literally only redirecting x.com/ to y.com/?)

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

RewriteEngine On
RewriteRule ^$ http://y.com/ [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 x.com.

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 (.*) http://y.com/$1 [R=301,L]

This will redirect x.com/ to y.com/ and x.com/file to y.com/file, but will not redirect x.com/subdir/file.php.

  • As I just added in the comments in the OP, when a user types in X.com, then it would go to Y.com. However, when an user adds a subdirectory to the URL, i.e. X.com/sub, then it would stay on X.com/sub. I've added a case to the OP to explain the situation, Thanks. May 13, 2014 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 x.com and y.com are separate hosting accounts?
    – MrWhite
    May 13, 2014 at 9:47
  • X.com -> Y.com; X.com/ANYsubdirectory -> X.com/ANYsubdirectory - one method for all subdirectories of X.com. And yes, X.com and Y.com are separated hosting accounts, right now. :) May 13, 2014 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?
    – MrWhite
    May 13, 2014 at 14:10
  • I think 'update #2' will perfectly fit. As X.com, X.com/home.html, and X.com/anotherfile.html will get redirected to Y.com incl. respected files (as in example further), while anything within a subdirectory stays on X.com. e.g. X.com/anotherfile.html goes to Y.com/anotherfile.html, while X.com/client1/index.html 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! =) May 13, 2014 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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