将CodeIgniter放入现有站点时如何设置htaccess

时间:2013-11-18 17:05:54

标签: .htaccess codeigniter migrate

我找到了答案,很惊讶我找不到这个具体情况:

我在http://www.mysites.com/site1/有一个现有的“直接”PHP站点,并希望随着时间的推移将其迁移到CodeIgniter。我知道有几种方法可以做到这一点,但我想单独保留现有站点,只需将CodeIgniter文件夹(CI)放在根目录(site1)中,然后将请求重定向到它,如果文件或目录没有不存在。

所以这个.htaccess文件位于site1文件夹中:

RewriteEngine on
RewriteBase /site1/CI/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ index.php/$1 [L]

此.htaccess文件位于site1 / CI文件夹中(需要?):

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ index.php/$1 [L]

和CodeIgniter config.php文件包含:

$config['base_url'] = 'http://www.mysites.com/site1/CI/';

如果我尝试去现有网站上的某个位置(例如http://www.mysites.com/site1/page.php),但是如果我尝试转到应该路由到CodeIgniter的页面(例如,{{3} }),我从CodeIgniter得到404错误。它似乎是在控制器'site1'中寻找方法'home'而不是控制器'home'。

所以我在根目录中尝试了这个.htaccess文件,以摆脱传递给index.php的'site1':

RewriteBase /site1/CI/
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule /site1/(?:(.*)) index.php/$1 [L]

不幸的是,这导致Apache返回404。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我终于完成了将CI应用程序放在一个文件夹级别的现有站点中的所有工作。我就是这样做的。

使用的文件夹结构:

  • SITE1
    • page1.php中
    • 使page2.php
    • 等...
    • CI
      • 应用
      • 资产
      • 系统
  • 站点2
  • site3
site1文件夹中的

.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond $1 !^(images|robots\.txt|assets)
RewriteRule ^(.*)$ CI/index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^assets
RewriteRule ^(.*)$ CI/$1 [L]

请注意,我从第一个块中的第三个RewriteCond中取出了通常的“index.php”(因为下面描述了HybridAuth和CodeIgniter路由)。第二个块是将CodeIgniter页面资产文件夹引用重定向到CI文件夹内的assets文件夹。 (否则我必须将assets文件夹移动到site1文件夹。)

这是config.php文件中的基本网址:

$config['base_url'] = 'http://www.mysites.com/site1/';

我必须将以下内容添加到CI配置文件夹中的routes.php文件中:

$route['site1/index\.php/(:any)'] = "$1";
$route['site1/(:any)'] = "$1";

即使在Apache重定向之后,CI仍然在URI中看到“site1”,因此,尝试加载显然不存在的“site1”控制器! 第二个 $ route []将“site1”带走。

我也在使用其回调中包含“index.php”的HybridAuth,因此CI会在URI中看到它并尝试加载“index.php”控制器,它显然也不存在! 第一个 $ route []将“site1 / index.php”带走。

希望这有助于某人。