Reputation: 2349
I am starting a project which will require 8 websites to be hosted from one server/IP, using the same (custom) framework.
What I need to do is use separate app folders for each site. This part is easy, just duplicate the folder structure. The part I am having trouble with is the .htaccess
.
Each application folder is set up like so:
- application
-controller
-view
-www
-css
-js
-img
Now, what needs to happen is that the requests for files need to be set up get them, and any request that is not a file goes to a bootstrap file for the individual application... Again this part is easy, as I have a .htaccess
file in the webroot to handle that once the request gets there.
What I need is, in the main root folder, to use .htaccess
to decide what application to go to based on the domain that the user visited.
When there is only one application folder to go to, my htaccess redirect section looks like this:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/application/www%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
RewriteRule ^$ application/www/ [L]
RewriteRule (.*) application/www/$1 [L]
basically, I need to replicate this for multiple domains using the same framework.
The application folders are named the same as the domain name, if that's any help!
Upvotes: 2
Views: 490
Reputation: 143886
Try this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}/www%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$/ [R=301,L]
RewriteRule ^$ application/www/ [L]
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}/www%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}/www%{REQUEST_URI} -f
RewriteRule ^(.*)$ application/www/$1 [L]
The 2 conditions above the last rule ensure that the requested resource actually exists in the application's www folder, otherwise the 404 message would expose the folder structure.
You may need to canonicalize the hostnames with some rules (above these) like this:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
to force hostnames to start with www or
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
to remove the www from the hostname.
Upvotes: 1