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.

0

5 Answers 5

8

What about simply:

Redirect /dir http://dir.example.com
2
  • 2
    This should be correct answer.
    – hakkikonu
    Sep 9, 2016 at 2:38
  • Use RedirectPermanent (or Redirect 301 or Redirect permanent) instead for a 301 redirect (as opposed to a 302 - temporary).
    – MrWhite
    Aug 2, 2020 at 16:11
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.

2
  • 2
    [R] yields 302 Moved Temporarily. You may want to use [R=301] to get 301 Moved Permanently.
    – Arjan
    Sep 8, 2009 at 20:09
  • 1
    Do it the 302 way until you're sure it's working, then switch to the 301.
    – TRiG
    Jul 4, 2011 at 10:51
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]
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
4
  • @earl had the .htaccess inside the /dir folder, so it basically does the same. (Except for making the www.-part optional.)
    – Arjan
    Sep 8, 2009 at 23:28
  • Yep, didn't notice that. We seldom use .htaccess so I'm always thinking of the main configuration file. Sep 9, 2009 at 7:27
  • 1
    Using .htaccess in this case saves you the trouble of having to match /dir/ explicitly.
    – earl
    Sep 9, 2009 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 2, 2020 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] 
1
  • 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 2, 2020 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .