e--
e--

Reputation: 198

.htaccess multiple-domain rewrite

I'm looking for a solution to support multiple domains on one webhosting account, which has to be done with htaccess. So the idea is, you call domainx and with htaccess the server "fakes" the webroot to a subfolder corresponding with the domain name. I allready have a "solution", but this doesn't work perfectly.

Problems I've got:

  1. Redirects through PHP (with base_url() of CodeIginiter), result in; for example "http://www.domein1.nl/domein1.nl/".
  2. It doesn't work on my local server.

So, the htaccess I'm currently using:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} domein1.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/domein1.nl/.*$
RewriteRule ^(.*)$ /domein1.nl/$1 [L]

RewriteCond %{HTTP_HOST} domein2.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/domein2.nl/.*$
RewriteRule ^(.*)$ /domein2.nl/$1 [L]

</IfModule>

The CodeIgniter PHP-code for the base_url(). The server variable "SCRIPT_NAME" adds the second domain folder, marked as problem 1. This should'nt happen if the root folder is faked correctly; but is that actually possible?

if (isset($_SERVER['HTTP_HOST']))
{
$base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}

And last but not least, it is'nt working on my local server while I do redirect through my hosts file:

192.168.2.9 local.domein1.nl
192.168.2.9 local.domein2.nl

Sooo.. How do I fix these problems? Thanks in advance!

Edit: The problem with my local server is fixed.. cough "sudo a2enmod rewrite" did the trick..

Edit2: Since stormdrain started about folder structure, here is mine to clarify the multiple CI applications.

Main .htaccess location / webroot

/public_html/.htaccess

domain1:

/application/domain1/ (domain1 application path)
/application/system/ (shared system path)
/public_html/domain1/index.php (CI domain1 index)

domain2:

/application/domain2/ (domain2 application path)
/application/system/ (shared system path)
/public_html/domain2/index.php (CI domain2 index)

Upvotes: 3

Views: 4250

Answers (2)

e--
e--

Reputation: 198

Well, it seems impossible to do neatly through htaccess..

O well, I "fixed" it. My fix within the CodeIgniter index.php: I replaced the declaration of the variable $application_folder with the code beneath.

define('DOMAIN', preg_replace(
    "/^(www.|local.)?([^.]+).[^.]+$/i", "\\2",
    $_SERVER['HTTP_HOST']
));

if ( DOMAIN == 'domain2')
    $application_folder = '../application/domain2';
else
    $application_folder = '../application/domain1';

I also made a minor change to the system "url_helper". Added this "static_url()" function, returning the URI to the path I save images/CSS/js etc.

if ( ! function_exists('static_url'))
{
    function static_url()
    {
        return base_url().'static/'.DOMAIN.'/';
    }
}

Only minor thing I've got to figure out is how to split up things like robots.txt

Upvotes: 1

stormdrain
stormdrain

Reputation: 7895

It's not totally clear what you are trying to do. Sounds like you have several domain names pointing to the same server in which you don't have full control and therefore can't set up VirtualHosts. You also want to serve the domains with a single application and not have a subfolder as part of the URL (e.g. you don't want http://www.domain1.nl/domain1.nl as the URL's home page).

If so, this might work:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_HOST} domein1.nl$ [NC]
    RewriteRule ^(.*)$ /domein1/$1 [L]

    RewriteCond %{HTTP_HOST} domein2.nl$ [NC]
    RewriteRule ^(.*)$ /domein2/$1 [L]

</IfModule>

Then create routes in CodeIgniter to route the request:

$route["/domein2/(:any)"] = "/domein2/$1";

To get the rewrites working locally, you need to add the domain to the .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_HOST} local.domein1.nl$ [NC]
    RewriteRule ^(.*)$ /domein1/$1 [L]

    RewriteCond %{HTTP_HOST} local.domein2.nl$ [NC]
    RewriteRule ^(.*)$ /domein2/$1 [L]

</IfModule>

Then base_url shouldn't need the SCRIPT_NAME since it is being rewritten and routed out of the URL:

if (isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST']. '/';
}

Update.

If you have a CI folder for each domain like:

/var/www/domain1/index.php
/var/www/domain1/application/
/var/www/domain1/system/
etc.

/var/www/domain2/index.php
/var/www/domain2/application/
/var/www/domain2/system/
etc.

RewriteCond %{HTTP_HOST} domain1.nl$ [NC]
RewriteRule ^(.*)$ /domain1/$1 [L]

RewriteCond %{HTTP_HOST} domain2.nl$ [NC]
RewriteRule ^(.*)$ /domain2/$1 [L]

Should do the job for you.

Upvotes: 2

Related Questions