You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter本地环境页面无法渲染问题求助

Troubleshooting CodeIgniter Local Setup: Runs But Won't Render Pages

Hey there, let's work through this issue—since your app works smoothly on production but hits snags locally, the problem is almost certainly tied to environment-specific configuration differences. Let's break down the fixes step by step:

1. Adjust the .htaccess RewriteBase

Looking at your provided .htaccess, the RewriteBase /sma/ line is commented out. On local servers (like XAMPP/WAMP), if your CodeIgniter app lives in a subfolder named sma under your localhost root, you need to uncomment this line to tell Apache where your app's root directory is:

DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sma/  # Uncomment this for local subfolder setup
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Keep this rule (the GoDaddy one is for shared hosting only)
RewriteRule ^(.*)$ index.php?$1 [NC,L,QSA]
</IfModule>

This fixes broken rewrite rules that can block controller access or asset loading.

2. Update CodeIgniter's Core Config

Head to application/config/config.php and tweak these two critical settings:

  • Set $config['base_url'] to match your local app URL:
    $config['base_url'] = 'http://localhost/sma/';
    
    Make sure this path exactly matches the RewriteBase you set in .htaccess.
  • Set $config['index_page'] to an empty string to remove index.php from URLs:
    $config['index_page'] = '';
    
    Leaving this as index.php will override your rewrite rules and cause routing issues.

3. Enable mod_rewrite Locally

Most local Apache servers (like XAMPP) have mod_rewrite disabled by default:

  • Open your Apache config file (usually httpd.conf in the apache/conf folder of your local server setup)
  • Find the line #LoadModule rewrite_module modules/mod_rewrite.so and remove the # to uncomment it
  • Restart Apache to apply the change

4. Fix Asset Paths for Local Environment

If your page loads but looks broken (no styles, images, or JS), it’s likely because asset paths are hardcoded for production. Use CodeIgniter's base_url() helper to reference assets dynamically:
Instead of hardcoding paths like:

<link rel="stylesheet" href="/assets/css/style.css">

Use this to ensure compatibility across environments:

<link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>">

5. Test Basic Routing to Isolate Issues

If pages still won’t render, test a simple controller to rule out routing problems. Create application/controllers/Test.php with this code:

<?php
class Test extends CI_Controller {
    public function index() {
        echo "Hello, local CodeIgniter!";
    }
}

Visit http://localhost/sma/test in your browser. If this works, the issue might be with specific controller methods or view file paths—double-check that view files exist in application/views/ and are loaded correctly with $this->load->view('view_name');.


内容的提问来源于stack exchange,提问作者veta 2020

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:12:57