How to root multiple domains to multiple subfolders

matty picture matty · Aug 9, 2013 · Viewed 14.1k times · Source

I have a setup where I have a couple of sites hosted in different subfolders in the root of my FTP


    ROOT
    -site1folder
    -site2folder

I have 2 domain names not associated with the account. I have changed the DNS records of the domains to point to my hosting account but I need the 2 different URLs to go to the correct subfolder to load the correct site rather than transfer the domains. The domains are

  • www.site1.com
  • www.site2.com

Ideally I would like site1 to load the contents of the site1folder and the same for site 2 and its corresponding folder.

I currently have a .htaccess file in my root which I have mashed together from looking at different posts. I am a newbie to .htaccess. I am currently trying to get site 1 to point to the site1 folder


    Options +FollowSymLinks

    #Turns the rewrite engine on.
    RewriteEngine on

    #Fix missing trailing slash character on folders.
    RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]

    RewriteBase /website1
    #www.site1.co.uk and site1.co.uk will map to the folder

    RewriteCond %{HTTP:Host} ^(?:www\.)?site1\.co.uk
    RewriteRule (.*) /site1 [NC,L,NS]

What happens when I load this is I get the contents of the site1 folder but at this URL site1.co.uk/site1

I don't want the URL to change at all. I just want it to load the contents of the subfolder it is pointed to.

Any ideas would be greatly appreciated even if its that it can't be dont and instead I should do it a different way.

Answer

matty picture matty · Aug 13, 2013

Ok people I managed to solve this one myself in the end after lots of head banging and reading a lot of the docs to decyfer others code. Here is what I did...



    Options +FollowSymLinks


    RewriteEngine on
    #========================================================================
    # FIRST Handle the http requests first before removing the additional url junk
    #========================================================================
    #rule for site1.com to link to site1folder directory
    RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
    RewriteCond %{REQUEST_URI} !^/site1folder/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /site1folder/$1
    #rule for site2.com to link to site2folder directory. Its the same as above just with site2 URL and subfolder
    RewriteCond %{HTTP_HOST} ^(www.)?site2.com$
    RewriteCond %{REQUEST_URI} !^/site2folder/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /site2folder/$1

    #==========================================================
    # SECOND Remove the additional url junk once the new url is loaded
    #==========================================================
    #rule for site1 url rewrite to remove /site1foler/index.php from the URL
    RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
    RewriteRule ^(/)?$ site1folder/index.php
    #rule for site2 url rewrite to remove /site2foler/index.php from the URL. Again its the same as above just with the site2 URL and sub folder info.
    RewriteCond %{HTTP_HOST} ^(www.)?site2.com$
    RewriteRule ^(/)?$ site2folder/index.php

Hopefully you can see where the repetition is in this so if you wished to add more sites down the line to it you could by copying and pasting the data and changing the site URL and sub folder to match the additional ones you wish to add.

I hope this has managed to help some of you. Also if you can see issues with this then it would be very much appreciated as I am a newbie at .htaccess files and as you can imagine, I figured as it works, it must be right.