Redirect a subdomain to a directory using .htaccess

I will tell you right off the bat that I don’t “get” regular expressions, and I don’t get .htacess rewrite rules. I wish I understood them better, but there’s some part of my brain that just fights me every time I try to get a better all-around understanding. Still, I have to (and want to) do stuff with htaccess, so I end up digging for code online, and trying stuff until something works. I wish I knew more about WHY it worked, but I’m just happy that it works at all. :)

Now that I’m done with my disclaimer, on to the point of this post. I had to use a htaccess file to redirect a subdomain to a directory in the main domain. For example: http://blog.mysite.com needed to point to http://www.mysite.com/blog/

After much digging and trial and error, this seems to work:RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mysite\.com
RewriteRule ^(.*)$ /blog/$1 [L]

10 Responses to “Redirect a subdomain to a directory using .htaccess”

  1. lucas Says:

    http://www.regular-expressions.info/

    that’s a good resource for regexp stuff…

  2. bubazoo Says:

    shoulda asked me, I’ve been using this bit of code for several years..

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^blog.mysite.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.blog.mysite.com$
    RewriteRule ^(.*)$ http://www.mysite.com/blog/1 [R=301,L]

    This performs a 301 Redirect on the subdomain. You have to check for http://www.blog or blog, because some browsers automatically put in the www at the beginning of each address.

  3. olmansju Says:

    so how would you use reg expressions to automate it for multiple sites?

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(whatIShere).mysite.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.(whatIShere).mysite.com$
    RewriteRule ^(.*)$ http://www.mysite.com/(whatIShere)/$1 [R=301,L]

    ? i tried but could not get it

    RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).mysite.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.([A-Za-z0-9]+).mysite.com$
    RewriteRule ^(.*)$ http://www.mysite.com/([A-Za-z0-9]+)/$1 [R=301,L]

  4. aequalsb Says:

    i concocted this htaccess file that will get the job done

    ==========================
    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.MYDOMAIN\.com.*$
    RewriteRule (.*) http://MYDOMAIN.com/blog/1 [L]

    ==========================
    i am very capable with regular expression because i simply hammered away at any given expression when the need arose. it was not easy — but i just kept on. try starting with little ones for practice and work your way up.

    now… htaccess files are still tough for me. i wrote my first one about a month ago. they are hard to fathom, and without knowing regular expressions, it’s even harder.

    let’s break down the htaccess file above:
    ——————–
    LINE 1: generic stuff… skipping
    ——————–
    LINE 2: generic stuff… skipping
    ——————–
    LINE 3: a rewrite cond :: in plain english -> take the dynamically provided HTTP_HOST and try to match it to the regular expression that follows it:

    ^ = start matching from the beginning of the string (aka: the “left edge” of the string)

    (www\.)? = optionally match the literal string “www.”
    “?” makes it optional and the parentheses tells the “?” to refer to the entire string “www.” (note the \ before the dot — it says to treat the “.” as a literal dot instead of a wildcard) (and you don’t need 2 lines of conditions as this expression will handle with and without “www.”)

    [^.]+\. = try to match ANYTHING that is NOT a dot 1 or more times then match a literal dot
    the brackets “[ ]” allow us to specify a class or range of characters, the “^” INSIDE the brackets says to NOT match the range, the “+” says “1 or more times” (so it must match AT LEAST one and optionally any number afterwards)(note: when a “.” is inside brackets it is automatically treated as a literal dot instead of a wildcard), then our literal dot “\.”

    MYDOMAIN\.com = match the literal string “MYDOMAIN\.com” where MYDOMAIN name = your actual domain name (don’t forget to escape the “\.”)

    .* = match ANYTHING “zero or more times”
    such as a trailing slash “/” — or query such as “?var=val” — or anchor such as “#anchor” — or anything that trails the domain part. the “.” tells it to be “anything” and the “*” tells it to be “zero or more times”

    $ = match to the end of the string
    technically not needed as the wildcard “.” will match anything until it runs out of string — but i think it’s tidy
    ——————–
    LINE 4: a rewrite rule :: rewrite the current URL, where the regular expression matches it, to the new URL that follows it

    (.*) = match anything “zero or more times” and capture it (using “( )” — it will be used in backreference $1)
    this part baffles me because i do not understand how it matches the part we need to remember. that is, the part trailing the domain name (if there is a trailing part) — my experience says it should match and capture the entire URL — but it works… (i do have an inkling that using the RewriteBase / is what makes it work)
    you do NOT need a “^” nor “$” because the wildcard “.” with “*” matches everything

    http://MYDOMAIN.com/blog/ = build the literal part of the URL (and, of course, “blog/” could be replaced with any filepath you wanted)

    $1 = append anything that may have been captured following the domain name from the original URL

    [L] = LAST = tell htaccess NOT to perform any more conditional checks nor rewrite rules — it is also technically NOT needed in an htaccess files where you have one or more conditions that trigger only ONE rewrite rule… as it will be the first and only rewrite rule performed

    that’s it…

  5. aequalsb Says:

    @lucas
    that IS a good website and their RegEx Buddy is a great regular expression learning/honing/testing software

    @olmansju
    i can see that your goal is to use the same file over and over for similar websites. unfortunately, i have been unable to find documentation or solution for capturing the current host to use it to rewrite URLs. however, i have been able to use this htaccess file for any hosting client of mine because it doesn’t rely on the hostname at all:
    the example is using the short and tidy /stats to redirect to a client’s webstats page at /plesk-stat/webstat
    ====================
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_URI} /stats/?$ [NC]
    RewriteRule . /plesk-stat/webstat/ [L]
    ====================
    it works, without alteration, for any client on my server

  6. Oleg Says:

    I’m in the same boat as you :D

    .htaccess and PHP regex I just don’t get. Whenever I need to use it for something, I google it.

  7. John Says:

    This works great except for one thing. When someone goes to http://www.mysite.com/ it does this http://mysite.com/www

    How do you fix this one?

  8. John Says:

    Also I found something that didn’t work on this:
    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.MYDOMAIN\.com.*$
    RewriteRule (.*) http://MYDOMAIN.com/blog/1 [L]

    I had to add parenthesis around (www\.)?[^.]+ to make it work so now it looks like this
    RewriteCond %{HTTP_HOST} ^((www\.)?[^.]+)\.MYDOMAIN\.com.*$

    AND I had to use a % insead of a $ at the end of the RewriteRule so it now looks like this
    RewriteRule (.*) http://MYDOMAIN.com/blog/%1 [L]

    Now it works except for when i go to http://www.MYDOMAIN.com i need it to go to http://www.MYDOMAIN.com/index.php and it isn’t doing that with this code.

  9. Subir Ghosh Says:

    Recently I removed some content from the main site (http://www.newswatch.in/) to a subdomain (http://oldcontent.newswatch.in/). Some content. Not all.

    What I am seeking to do is something like this:

    Redirect http://www.newswatch.in/news-analyses/*.* TO http://oldcontent.newswatch.in/news-analyses/*

    For this I tried using:

    Redirect /news-analyses http://oldcontent.newswatch.in/news-analyses/

    This is not working because the server is creating a loop for http://oldcontent.newswatch.in/news-analyses/ as well.

    How should this be changed?

  10. animesh Says:

    i have been trying for several day and still unaware to solve the sub domain
    problem.
    the code as follow i have use as far got help from different source.

    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/index\.php
    RewriteCond %{HTTP_HOST} !^www\.sportalocity\.com
    RewriteCond %{HTTP_HOST} ^([^.]+)\.sportalocity\.com
    RewriteRule ^([^.]+) /index.php?sitename=%1 [L]

    RewriteCond %{REQUEST_URI} !^/index\.php
    RewriteCond %{HTTP_HOST} !^www\.sportalocity\.com
    RewriteRule ^([^.]+) /index.php?sitename=”home” [L]

Leave a Reply

Subscribe without commenting