htaccess的多域重写 [英] .htaccess multiple-domain rewrite

查看:245
本文介绍了htaccess的多域重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案,以支持在一个虚拟主机帐户,该帐户必须与htaccess的做多域。这样的想法是,你叫domainx和htaccess的服务器假货的根目录对应的域名的子文件夹。我媒体链接有一个解决方案,但这并不能很好地工作。

问题我有:

  1. 在重定向通过PHP(用$ BASE_URL()C $ cIginiter),导致;例如,http://www.domein1.nl/domein1.nl/。
  2. 在它没有我的本地服务器上运行。

所以,我目前使用的htaccess的:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{HTTP_HOST} domein1.nl $ [NC]
的RewriteCond%{REQUEST_URI}!^ / domein1.nl/.*$
重写规则^(。*)$ /domein1.nl/$1 [L]

的RewriteCond%{HTTP_HOST} domein2.nl $ [NC]
的RewriteCond%{REQUEST_URI}!^ / domein2.nl/.*$
重写规则^(。*)$ /domein2.nl/$1 [L]

< / IfModule>
 

的codeIgniter PHP的code为BASE_URL()。服务器变量SCRIPT_NAME中添加第二个站点文件夹,标记为问题1.如果这should'nt发生根文件夹被正确伪造的;但实际上可能吗?

 如果(使用isset($ _ SERVER ['HTTP_HOST']))
{
$ BASE_URL =使用isset($ _ SERVER ['HTTPS'])及和放大器;用strtolower($ _ SERVER ['HTTPS'])!=='关'? HTTPS:HTTP;
。$ BASE_URL ='://。 $ _ SERVER ['HTTP_HOST'];
。$ BASE_URL = str_replace函数(基名($ _ SERVER ['SCRIPT_NAME']),'',$ _ SERVER ['SCRIPT_NAME']);
}
 

最后但并非最不重要的,它is'nt我的工作,而我也通过重定向我的主机本地服务器上的文件:

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

的sooo ...我该如何解决这些问题?在此先感谢!

编辑:用我的本地服务器的问题是固定的。咳嗽的命令a2enmod重写的伎俩。

EDIT2:由于stormdrain开始有关文件夹结构,这里是我澄清多个CI应用

主要的.htaccess位置/ Webroot公司

  /public_html/.htaccess
 

域1:

  /应用/域1 /(domain1的应用程序路径)
/应用/系统/(共享系统路径)
/public_html/domain1/index.php(CI DOMAIN1指数)
 

DOMAIN2

  /应用/域2 /(域2应用程序路径)
/应用/系统/(共享系统路径)
/public_html/domain2/index.php(CI DOMAIN2指数)
 

解决方案

嗯,这似乎是不可能通过htaccess的做到整齐..

不过,噢,我的固定了。 我的codeIgniter index.php文件内修复:我替换的变量$ application_folder与code下的申报

 定义('域',preg_replace(
    /^(www.|local.)?([^.]+).[^.]+$/i,\\ 2,
    $ _ SERVER ['HTTP_HOST']
));

如果(域=='域2)
    $ application_folder ='../application/domain2';
其他
    $ application_folder ='../application/domain1';
 

我还做了一个小的改动系统url_helper。添加了这个static_url()功能,该URI回到我保存图像的路径/ CSS / JS等。

 如果(!function_exists(static_url'))
{
    功能static_url()
    {
        返回BASE_URL()'静电/'域'/'。;
    }
}
 

只有轻微的事情,我一定要弄清楚是如何分割之类的东西的robots.txt

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)

解决方案

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

这篇关于htaccess的多域重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆