简体   繁体   English

.htaccess 多域重写

[英].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.我正在寻找一种解决方案来支持一个虚拟主机帐户上的多个域,这必须使用 htaccess 来完成。 So the idea is, you call domainx and with htaccess the server "fakes" the webroot to a subfolder corresponding with the domain name.所以这个想法是,你调用 domainx 并使用 htaccess 服务器将 webroot“伪造”到与域名对应的子文件夹。 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;通过 PHP 重定向(使用 CodeIgniter 的 base_url()),结果为; for example "http://www.domein1.nl/domein1.nl/".例如“http://www.domein1.nl/domein1.nl/”。
  2. It doesn't work on my local server.它在我的本地服务器上不起作用。

So, the htaccess I'm currently using:所以,我目前使用的 htaccess:

<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(). base_url() 的 CodeIgniter PHP 代码。 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;服务器变量“SCRIPT_NAME”添加了第二个域文件夹,标记为问题 1。如果根文件夹被正确伪造,则不会发生这种情况; 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? Sooo.. 我该如何解决这些问题? Thanks in advance!提前致谢!

Edit: The problem with my local server is fixed.. cough "sudo a2enmod rewrite" did the trick..编辑:我的本地服务器的问题已修复..咳咳“sudo a2enmod rewrite”成功了..

Edit2: Since stormdrain started about folder structure, here is mine to clarify the multiple CI applications. Edit2:由于stormdrain 开始是关于文件夹结构的,这里是我来澄清多个CI 应用程序的。

Main .htaccess location / webroot主 .htaccess 位置 / webroot

/public_html/.htaccess

domain1:域 1:

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

domain2:域2:

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

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.听起来您有多个域名指向同一台服务器,您无法完全控制这些服务器,因此无法设置 VirtualHosts。 You also want to serve the domains with a single application and not have a subfolder as part of the URL (eg you don't want http://www.domain1.nl/domain1.nl as the URL's home page).您还希望使用单个应用程序为域提供服务,并且不将子文件夹作为 URL 的一部分(例如,您不希望http://www.domain1.nl/domain1.nl作为 URL 的主页)。

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:然后在 CodeIgniter 中创建路由来路由请求:

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

To get the rewrites working locally, you need to add the domain to the .htaccess file:要在本地进行重写,您需要将域添加到.htaccess文件中:

<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:然后 base_url 不需要 SCRIPT_NAME 因为它正在被重写并从 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:如果每个域都有一个 CI 文件夹,例如:

/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.应该为你做这项工作。

Well, it seems impossible to do neatly through htaccess..好吧,似乎不可能通过 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.我在 CodeIgniter index.php 中的修复:我用下面的代码替换了变量 $application_folder 的声明。

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".我还对系统“url_helper”做了一个小改动。 Added this "static_url()" function, returning the URI to the path I save images/CSS/js etc.添加了这个“static_url()”函数,将URI返回到我保存图像/CSS/js等的路径。

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我唯一需要弄清楚的是如何拆分 robots.txt 之类的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM