Mobile app version of vmapp.org
Login or Join
Gloria169

: 301 Redirect a domain to another domain, but keeping the subdirectories the same Is it possible to configure a top-domain (http://x.com) (DNS/.htaccess/else?) to redirect to another domain (http://y.com),

@Gloria169

Posted in: #301Redirect #Dns #Domains #Htaccess

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 x.com/subdir/index.html, you'd see the files in the subdirectory of 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. :)

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Gloria169

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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 (.*) y.com/ [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, x.com/foo/bar redirects to 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 x.com/ (and only the root URL) to y.com. Anything else eg. x.com/something stays at x.com.

RewriteEngine On
RewriteRule ^$ 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 (.*) y.com/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme