7

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

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

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Thody
  • 183
  • 3
  • 3
  • 5

5 Answers5

8

What about simply:

Redirect /dir http://dir.example.com
MrWhite
  • 12,647
  • 4
  • 29
  • 41
mdpc
  • 11,856
  • 28
  • 53
  • 67
7

Add a .htaccess to the directory belonging to www.example.com/dir:

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

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

MrWhite
  • 12,647
  • 4
  • 29
  • 41
earl
  • 2,971
  • 24
  • 16
1

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]
Gumbo
  • 436
  • 2
  • 6
0

djhowell was almost there:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/dir(/.*)?$ http://dir.example.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 to the end of the URI. (This is important -- without it we'd also match www.example.com/dirsomethingelse).

  • www.example.com/dir -> dir.example.com
  • www.example.com/dir/ -> dir.example.com/
  • www.example.com/dir/blah.html -> dir.example.com/blah.html
MrWhite
  • 12,647
  • 4
  • 29
  • 41
markdrayton
  • 2,449
  • 1
  • 20
  • 24
  • @earl had the .htaccess *inside* the /dir folder, so it basically does the same. (Except for making the `www.`-part optional.) – Arjan Sep 08 '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 09 '09 at 07:27
  • 1
    Using `.htaccess` in this case saves you the trouble of having to match `/dir/` explicitly. – earl Sep 09 '09 at 18:11
  • "You need the leading slash in the `RewriteRule` or nothing will match." - This is only if the directives are being used in a _server_ or _virtualhost_ context. The OP is suggesting the use of `.htaccess` (_directory_ context) - in this case the leading slash should be omitted. – MrWhite Aug 02 '20 at 12:53
-1

This should work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^dir/(.*) http://dir.example.com/$1 [R=301,L] 
MrWhite
  • 12,647
  • 4
  • 29
  • 41
djhowell
  • 1,192
  • 7
  • 9
  • Additional explanation is required. This won't work if it was to be used in the `.htaccess` inside the `/dir` subdirectory as suggested in the question. If you include the `dir/` in the `RewriteRule` _pattern_ then it will need to go in the `.htaccess` file in the _document root_. – MrWhite Aug 02 '20 at 12:45