Take the 2-minute tour ×
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 (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?

summarized
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. :)

share|improve this question
    
It is possible. What part are you stuck on? –  JamesRyan May 12 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 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 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. –  Sander Schaeffer May 13 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 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.

share|improve this answer
    
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. –  Sander Schaeffer May 13 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? –  w3d May 13 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. :) –  Sander Schaeffer May 13 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 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! =) –  Sander Schaeffer May 13 at 18:31

Your Answer

 
discard

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.