【问题标题】:How to set htaccess when putting CodeIgniter into existing site将 CodeIgniter 放入现有站点时如何设置 htaccess
【发布时间】:2014-03-07 10:52:31
【问题描述】:

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

我在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 的页面(例如,http://www.mysites.com/site1/home),我从 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。

有什么想法吗?谢谢!

【问题讨论】:

    标签: .htaccess codeigniter migrate


    【解决方案1】:

    我终于完成了将 CI 应用程序放入现有站点一层文件夹深度的所有工作。这是我的做法。

    使用的文件夹结构:

    • 站点1
      • page1.php
      • page2.php
      • 等等……
      • CI
        • 应用
        • 资产
        • 系统
    • 站点2
    • 站点3

    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 文件夹移动到 site1 文件夹。)

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

    $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”。

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

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 2023-03-06
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多