Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

A client had an application at www.domain.com/dir. They have now set up a subdomain at dir.domain.com. The subdomain references the files stored at www.domain.com/dir. They'd now like people accessing www.domain.com/dir to be redirected to dir.domain.com.

I'm guessing I need a .htaccess to sit in www.domain.com/dir and if it's accessed via with www, redirect to the subdomain, just not sure of the syntax.

share|improve this question

5 Answers 5

Add a .htacess to the directory belonging to www.domain.com/dir:

RewriteEngine on
RewriteCond ${HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*) http://dir.domain.com/$1 [R,L]

The first line enables mod_rewrite, the second line checks that the current request is using hostname www.domain.com to access the resource, and the third line redirects all such requests to the desired target hostname.

share|improve this answer
1  
[R] yields 302 Moved Temporarily. You may want to use [R=301] to get 301 Moved Permanently. –  Arjan Sep 8 '09 at 20:09
    
Do it the 302 way until you're sure it's working, then switch to the 301. –  TRiG Jul 4 '11 at 10:51

djhowell was almost there:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^/dir(/.*)?$ http://dir.domain.com$1 [R=301,L]

You need the leading slash in the RewriteRule or nothing will match. The (/.*)?$ bit means match 0 or 1 slash-then-anything chunks then then end of the URI. (This is important -- without it we'd also match www.domain.com/dirsomethingelse).

  • www.domain.com/dir -> dir.domain.com
  • www.domain.com/dir/ -> dir.domain.com/
  • www.domain.com/dir/blah.html -> dir.domain.com/blah.html
share|improve this answer
    
@earl had the .htaccess inside the /dir folder, so it basically does the same. (Except for making the www.-part optional.) –  Arjan Sep 8 '09 at 23:28
    
Yep, didn't notice that. We seldom use .htaccess so I'm always thinking of the main configuration file. –  markdrayton Sep 9 '09 at 7:27
    
Using .htaccess in this case saves you the trouble of having to match /dir/ explicitly. –  earl Sep 9 '09 at 18:11

What about simply:

Redirect /dir http://dir.domain.com
share|improve this answer

Try this rule in a .htaccess file in your /dir directory:

RewriteCond %{HTTP_HOST} !=dir.example.com
RewriteRule .* http://dir.example.com/$0 [L,R=301]

Or this rule in your root directory:

RewriteCond %{HTTP_HOST} !=dir.example.com
RewriteRule ^dir(/(.*))?$ http://dir.example.com/$2 [L,R=301]
share|improve this answer

This should work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^dir/(.*) http://dir.domain.com/$1 [R=301,L]
share|improve this answer

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.