Brob
Brob

Reputation: 675

htaccess rewrite to sub-domain

Hi I have a independent site that sits within a sub-directory of a main server.

All links within the sub-directory site point to the root and I'd like to capture those and send them back to the sub-directory rather than the root.

Is this possible with htaccess?

Upvotes: 1

Views: 216

Answers (2)

Trott
Trott

Reputation: 70055

As @Gerben pointed out (whose answer you should totally go and upvote right now) you can make the other domain behave as if the subdir is the document-root like this:

RewriteEngine on
RewriteBase     /

RewriteCond %{HTTP_HOST} ^seconddomain.example.com$ [NC]
RewriteCond $1 !^subdir
RewriteRule ^(.*)$ /subdir/$1 [L]

Another way to do what you are asking about is to use a <base> tag in your HTML documents. One advantage of this is that you don't have extra .htaccess processing for every single document served by the web server (including all documents not in the subdirectory/subdomain).

If you want to do it inside .htaccess and have it redirect regardless of whether or not it is the subdomain, it would look this:

RewriteEngine on
RewriteCond %{PATH_INFO} !^subdir/
RewriteRule ^(.*)$ subdir/$1 [L]

Upvotes: 1

Gerben
Gerben

Reputation: 16825

This will not redirect to the subdir, but make the other domain behave as if the subdir is the document-root. Which I think is even better.

RewriteEngine on
RewriteBase     /

RewriteCond %{HTTP_HOST} ^seconddomain.nl$ [NC]
RewriteCond $1 !^subfolder
RewriteRule ^(.*)$ /subfolder/$1 [L]

Upvotes: 2

Related Questions