Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to root multiple domains to multiple subfolders

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.

like image 949
matty Avatar asked Aug 09 '13 18:08

matty


People also ask

How do I redirect a URL to a subdirectory?

Redirect "ALL" requests to a domain to a subdirectory You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload. Once connected, upload (or create) a text file named .

How do I create a subfolder domain?

Make a subdirectory in your WWW root dir that can be accessed by the user that your webserver runs under with the name that you want to put after your domain (ex: www.mysite.com/thisisthedirnameyouuse/afile.html ), and put the files in there. Ryan J means 'make a new folder' when he says 'subdirectory'.

How do I redirect a folder?

Right-click a folder that you want to redirect (for example, Documents), and then select Properties. In the Properties dialog box, from the Setting box, select Basic - Redirect everyone's folder to the same location.


1 Answers

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.

like image 200
matty Avatar answered Sep 22 '22 18:09

matty