CodeIgniter迁移至A2主机后Base URL失效问题咨询
Hey Emily, let's work through this base URL problem you're hitting—this is super common when testing migrated sites with IP addresses instead of a proper domain, especially with older CodeIgniter versions (since your site's 5 years old, it's likely CI 2.x or early 3.x, which are pickier about base_url config).
Here are the steps to get your CSS loading correctly:
1. Lock in a Precise Base URL (No Dynamic Variables for Now)
Since you're accessing the site via an IP + subdirectory (and haven't updated DNS yet), dynamic variables like $_SERVER['SERVER_NAME'] won't work reliably—SERVER_NAME pulls the server's hostname, not the IP you're using to access it. Stick with a hardcoded value first to eliminate variables:
// application/config/config.php $config['base_url'] = 'http://xxx.xxx.xx.xxx/blah.com/';
⚠️ Don't forget the trailing slash—CodeIgniter relies on this to build correct paths for assets.
2. Ensure All Assets Use base_url()
The CSS is missing because your old site might be using relative paths (like assets/css/style.css) or hardcoded domain URLs. Update every asset reference to use CodeIgniter's base_url() helper function:
// Bad: Relative path that breaks in subdirectories <link rel="stylesheet" href="assets/css/style.css"> // Good: Uses your configured base_url to build the full path <link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>">
If your site has a lot of these references, do a quick search/replace across your views to fix them all at once.
3. Fix Your .htaccess (If Using URL Rewriting)
If your old site uses clean URLs (no index.php in the address), your .htaccess file needs a RewriteBase line pointing to your subdirectory. Update it to:
RewriteEngine On RewriteBase /blah.com/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
This tells Apache to route all requests through the blah.com subdirectory, which prevents broken paths for assets and controller routes.
4. Test with HTTP_HOST if You Want Dynamic IP Handling
If you don't want to hardcode the IP (maybe you switch between local and server testing), use $_SERVER['HTTP_HOST'] instead—it pulls the exact address you used to access the site (IP included):
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/blah.com/';
This works better than SERVER_NAME when accessing via IP, since HTTP_HOST reflects the client's actual request URL.
5. Debug with Browser Dev Tools
If CSS is still missing, open your browser's DevTools (F12) and go to the Network tab. Refresh the page and check the status of your CSS files—if they're returning 404 errors, look at the requested URL. It'll show you exactly where CodeIgniter is trying to load the CSS from, which will point you to whether the base_url is wrong or the asset path is misconfigured.
Once you update your DNS later, you can just swap the base_url to your actual domain (http://blah.com/) and everything should keep working.
内容的提问来源于stack exchange,提问作者Emily

