【问题标题】:Calling CodeIgniter get_instance in Wordpress causes 404在 Wordpress 中调用 CodeIgniter get_instance 导致 404
【发布时间】:2015-10-05 05:41:07
【问题描述】:

过去一天我一直在努力让 Wordpress 与 Codeigniter 完美结合。我有一个用 Codeigniter 编写的父应用程序,我试图用它来控制对 Wordpress 安装的访问。

到目前为止,我很幸运地获得了一个 Codeigniter 实例(通过 get_instance())显示并进行了必要的修改,以使这两个应用程序在大多数情况下不会相互干扰。

但是,对于 Wordpress 站点的根 index.php 之外的任何内容,我现在都碰壁了。

URL 类似于 /parent/content/external/wordpress,其中“parent”是 Codeigniter 根文件夹,而 wordpress 是 Wordpress 根文件夹。

我可以加载 example.com/parent/content/external/wordpress(带或不带 index.php),但 wordpress/sample-page 之类的任何内容都会导致 Codeigniter 404 错误。

似乎 Codeigniter 正在尝试将 URL 解析为控制器和方法,但失败了。我的 CI 日志中出现 404 错误,这些错误指向 Wordpress url 的第一段(上例中的示例页面)。

有人知道我如何让 CI 在通过推入 Wordpress index.php 文件的代码加载时停止尝试解析吗?

该文件的内容粘贴在下面。谢谢!

<?php

// BEGIN: CodeIgniter setup

// Remove the query string
$_SERVER['QUERY_STRING'] = '';  

// Include the codeigniter framework
define("REQUEST", "external");
require('/home/magicmag/magicmagazine.com/x/index.php');

// Create CI instance
$CI =& get_instance();

// Authenticate user with custom redirect
$CI->user = $CI->xlib->isLoggedIn(FALSE);

// Custom redirect
if(!$CI->user) {
    redirect('/user/login?d=/content/ext/magic-live-2015');
}

#ToDo: Rewrite this so that the destination is determined by current URL or by the starting URL of the external content

// Check for existing auth
if(!isset($CI->user['ext-magic-live-2015'])) {
    // User isn't currently authenitcated to the external content

    // Try to authentical
    $CI->user['ext-magic-live-2015'] = $CI->xlib->hasExtAccess($CI->user['userId'], '1'); // MAGIC Live is extId 1

    // If authenticated
    if($CI->user['ext-magic-live-2015']) {

        // Store it in the session
        $CI->session->set_userdata('ext-magic-live-2015', $CI->user['ext-magic-live-2015']); #ToDo fix hardcoded name
    } else {
        // Not authenticated
        $CI->session->set_flashdata('danger', 'You do not have access to that content.');
        redirect('/');
    }
}


// END: CodeIgniter setup

// UNEDITED WORDPRESS INDEX.PHP IS BELOW

/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */

define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

这里是 Codeigniter .htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /x/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

【问题讨论】:

  • 您能否发布您的 CI .htaccess 以及它可能在那里,我们需要从重写条件中排除您的 WP 路径
  • 当然可以。刚刚编辑了问题以包含 CI .htaccess。
  • 您找到解决问题的方法了吗?

标签: php wordpress .htaccess codeigniter


【解决方案1】:

好的,你的 .htaccess 不允许访问 word press 目录,并且会触发任何 URI_REQUEST 到 codeigniter index.php 所以为了允许访问 wp 目录添加一个新的条件+规则,像这样

#allow access to wp directory 
RewriteCond %{REQUEST_URI} ^parent/content/external/wordpress.*
RewriteRule ^(.*)$ /$1 [L]

所以你的 .htaccess 看起来像这样

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /x/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #allow access to wp directory 
    RewriteCond %{REQUEST_URI} ^parent/content/external/wordpress.*
    RewriteRule ^(.*)$ /$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

你可以在这里测试这个 .htaccess http://htaccess.madewithlove.be/

当您请求时 例如,parent/content/external/wordpress/11/22/33

您将被重定向到 wp 而不会被 ci index.php“捕获”

如果它仍然不起作用,就玩这条线

RewriteCond %{REQUEST_URI} ^parent/content/external/wordpress.*

一点点

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 2015-04-07
    • 1970-01-01
    • 2012-11-20
    • 2017-10-29
    • 2011-10-08
    • 2023-03-11
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多