gpt4 book ai didi

php - .htaccess 多域重写

转载 作者:搜寻专家 更新时间:2023-10-31 20:45:58 24 4
gpt4 key购买 nike

我正在寻找一种解决方案来支持一个虚拟主机帐户上的多个域,这必须使用 htaccess 来完成。所以这个想法是,你调用 domainx 并使用 htaccess 服务器将 webroot “伪造”到与域名对应的子文件夹。我已经有了一个“解决方案”,但这并不完美。

我遇到的问题:

  1. 通过 PHP 重定向(使用 CodeIginiter 的 base_url()),导致;例如“http://www.domein1.nl/domein1.nl/”。
  2. 它在我的本地服务器上不起作用。

那么,我目前使用的 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>

base_url() 的 CodeIgniter PHP 代码。 服务器变量“SCRIPT_NAME”添加了第二个域文件夹,标记为问题 1。如果正确伪造了根文件夹,则不会发生这种情况;但这真的可能吗?

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']);
}

最后但同样重要的是,当我通过我的主机文件进行重定向时,它无法在我的本地服务器上运行:

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

Sooo.. 我该如何解决这些问题?提前致谢!

编辑:我本地服务器的问题已修复。咳咳“sudo a2enmod rewrite”成功了。

编辑 2:由于 stormdrain 开始介绍文件夹结构,我在这里澄清多个 CI 应用程序。

主要的 .htaccess 位置/webroot

/public_html/.htaccess

域 1:

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

域 2:

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

最佳答案

目前还不完全清楚您要做什么。听起来您有几个域名指向同一台服务器,您在其中没有完全控制权,因此无法设置 VirtualHosts。您还希望使用单个应用程序为域提供服务,并且不希望将子文件夹作为 URL 的一部分(例如,您不希望 http://www.domain1.nl/domain1.nl 作为 URL 的主页)。

如果是这样,这可能有效:

<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>

然后在 CodeIgniter 中创建路由来路由请求:

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

要使重写在本地工作,您需要将域添加到 .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>

那么 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']. '/';
}

更新。

如果每个域都有一个 CI 文件夹,例如:

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

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

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

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

应该为您完成这项工作。

关于php - .htaccess 多域重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13309999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com