Go Back   SZONE.US Forums > Do it yourself (DIY) > Web Tips

Web Tips Tips and tricks specializing in programming languages of the web. ie. PHP, MYSQL, XHTML, CSS, etc

How to redirect domains to their own sub-directory

Views:421
Reply
Thread Tools Search this Thread Rate Thread
  #1  
Unread 02.28.17, 01:43 PM
Zachariah Boren's Avatar
Zachariah Boren Zachariah Boren is offline
Administrator
 
Join Date: 02.07
Location: Canoga Park, CA
Posts: 928
Blog Entries: 6
Images: 10075
Send a message via ICQ to Zachariah Boren Send a message via AIM to Zachariah Boren Send a message via MSN to Zachariah Boren Send a message via Yahoo to Zachariah Boren Send a message via twitter to Zachariah Boren
How to redirect domains to their own sub-directory

I worked on a shared website hosting account recently setting up multiple domains to work from their own sub-folders on a single public directory. The hosting was on a Linux server running Apache, MySQL with cPanel access. On this hosting plan you can host 10 domains from the same account. It seems less messy on the file management side to organize each domain into their own sub-directory on the server.

It was quite easy once you think about it.

REFF:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Desired layout of file structure on the web server.
  • /public_html
  • domain1.com => /public_html/site1
  • domain2.com => /public_html/site2
  • etc

I ended up pointing all domains in cPanel to a single location /public_html.
  • site1.com => /public_html
  • site2.com => /public_html

I then created a .htaccess file and uploaded it to /public_html that will push requests to the correct subfolders where each website is contained.

EX:
  • requests for site1.com will go to /public_html/site1
  • requests for site2.com will go to /public_html/site2


.htaccess
Code:
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} !^/public_html/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1

#rule for site2.com to link to site2folder directory. Its the same as above just with site2 URL and sub-folder
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$
RewriteCond %{REQUEST_URI} !^/public_html/site2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site2/$1

#==========================================================
# SECOND Remove the additional url junk once the new url is loaded
#==========================================================

#rule for site1 url rewrite to remove /site1folder/index.php from the URL
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php

#rule for site2 url rewrite to remove /site2folder/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 ^(/)?$ site2/index.php


Future domains can be added at a later date:
EX:
  • domain3.com => /public_html/site3
  • domain4.com => /public_html/site4
  • domain5.com => /public_html/site5
  • domain6.com => /public_html/site6
  • domain7.com => /public_html/site7
  • domain8.com => /public_html/site8
__________________
http://www.szone.us | http://www.gzhq.net
Twitter | Facebook | My:Hacks @ vBulletin.org
Member of Kiwanis Club of Chatsworth
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:



All times are GMT -8. The time now is 05:10 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2007 - 20017 SZONE.US All rights reserved