CodeIgniter本地环境页面无法渲染问题求助
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:
Make sure this path exactly matches the$config['base_url'] = 'http://localhost/sma/';RewriteBaseyou set in .htaccess. - Set
$config['index_page']to an empty string to removeindex.phpfrom URLs:
Leaving this as$config['index_page'] = '';index.phpwill 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.confin theapache/conffolder of your local server setup) - Find the line
#LoadModule rewrite_module modules/mod_rewrite.soand 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

