douweida2878 2015-07-15 23:32
浏览 74

在Wordpress中调用CodeIgniter get_instance会导致404

I've been working for the past day or so on getting Wordpress the play nicely with Codeigniter. I have a parent application written in Codeigniter that I'm trying to use to control access to a Wordpress install.

I've had okay luck so far with getting a Codeigniter instance (via get_instance()) to show up and made the necessary modifications to get the two applications to stay out of each other's way for the most part.

However, I'm up against a wall now with regard to anything past the root index.php of the Wordpress site.

URL's look like /parent/content/external/wordpress where "parent" is the Codeigniter root folder and wordpress is the Wordpress root folder.

I can load up example.com/parent/content/external/wordpress (with or without index.php) but anything like wordpress/sample-page results in a Codeigniter 404 error.

It seems like Codeigniter is trying to resolve the URL to a controller and method, but failing. I am getting 404 errors in my CI logs that point to the first segment of the Wordpress url (sample-page in the example above).

Does anybody know how I might get CI to stop trying to resolve when loaded via the code that was shoved into the Wordpress index.php file?

Contents of that file pasted below. Thank you!

<?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' );

And here is the Codeigniter .htaccess file:

<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>
  • 写回答

1条回答 默认 最新

  • duanniubeng2265 2015-07-17 19:21
    关注

    ok your .htaccess does not allow accessing the word press directory and will trigger any URI_REQUEST to codeigniter index.php so in order to allow access to the wp directory add a new condition + rule like so

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

    so your .htaccess will look like this

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

    you can test this .htaccess here http://htaccess.madewithlove.be/

    when you request for example, parent/content/external/wordpress/11/22/33

    you will be redirected to wp without being 'caught' by ci index.php

    if it still doesn't work just play with the line

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

    a little bit

    hope that helps

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端