Rewrite subdomain to subdirectory

Discussion in 'Apache' started by foobaa, Aug 23, 2009.

  1. #1
    Hi,

    For the first time ever I think I'm understanding how mod_rewrite works! It's just regexps with a twist. Saying that, I seem to be completely stumped by quite a simple problem.

    www.mysite.com points to /var/www/mysite
    area1.mysite.com points to /var/www/mysite/area1

    What I'm trying to do is have:
    area1.mysite.com rewritten to www.mysite.com/area.php?area=area1

    using .htaccess either in the mysite directory or area1 subdirectory.

    My two closest attempts are:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^area1 [NC]
    RewriteRule ^(.*)$ /area\.php\?area\=area1

    (Placed in /var/www/mysite) Which gives a 500 error - checking the log, it goes into a loop which makes sense. But at the same time I'm sure I've followed examples I've seen around and it should work?

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^area1 [NC]
    RewriteRule ^(.*)$ http://www.mysite.com/area\.php\?area\=area1

    But that redirects instead of rewriting.

    What's the solution?

    Thanks!

    Edit: Sorry - I realise my title wasn't accurate - not thinking straight
     
    Last edited: Aug 23, 2009
    foobaa, Aug 23, 2009 IP
  2. renownedmedia

    renownedmedia Well-Known Member

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #2
    I believe you are going to need to redirect to the new domain and lose the area1 from your url.

    The way a rewrite works is that it changes the url and keeps it hidden from the user. So, you can't redirect to the parent directory from the base of the domain (area1.mysite.com/../ doesn't work) and you can't rewrite to a separate domain (mysite.com vs area1.mysite.com).

    What you COULD do is set up a .htaccess that does the following three things on area1.mysite.com:
    1. Take the url that it is sent
    2. Take the useful data from the end (?x=1&y=2)
    3. Send that data to a php file
    4. fopen() or cURL the URL that you want to be displayed using your php file and display the content to the end user

    But, this wouldn't work good with forms or cookies... Just urls with GET information or displaying page content.
     
    renownedmedia, Aug 24, 2009 IP
  3. foobaa

    foobaa Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you! :) I will have a rethink and find a way of reorganising the structure. I had a bad feeling that I was trying to do something that wasn't possible. I still need to fully absorb what you said but I'm guessing that the limitation of being able to rewrite accross (sub)domains is not because I'm using an htaccess file rather than specifying the rewrite rules in a server-wide file? I'm not keen on using cURL since I'm sure the overhead would be a lot greater than what mod_rewrite could achieve, and adds a whole layer to the system which could confuse stats among other things, like cookies and forms as you say. Thanks again!
     
    foobaa, Aug 24, 2009 IP