Codeigniter本地localhost访问重定向至线上服务器问题求助
Hey there, let's tackle that frustrating redirect issue you're hitting with your offline CodeIgniter setup. I’ve run into this exact problem countless times when moving live sites to local environments, so here’s a step-by-step breakdown to fix it:
config.php This is the most common culprit. CodeIgniter uses the base URL to build links and handle routing, so if it’s set to your live site, it’ll redirect every time.
- Navigate to
application/config/config.phpin your local project - Search for the line
$config['base_url']— it’ll probably be set to your live site’s full URL (likehttps://your-online-site.com) - Replace it with your local URL. If your project is in the root of your local server, use
http://localhost. If it’s in a subfolder (e.g.,localhost/codeigniter-project), usehttp://localhost/codeigniter-project/ - Pro tip: If you want CodeIgniter to auto-detect the local URL, you can set it to an empty string:
$config['base_url'] = '';
Some developers hardcode live URLs in controllers, views, or helpers instead of using CodeIgniter’s built-in URL functions.
- Do a global search across your project files for your live site’s URL (e.g.,
https://your-online-site.com) - Replace any hardcoded redirects (like
redirect('https://your-online-site.com/dashboard')) with relative paths usingsite_url():redirect(site_url('dashboard')) - Check view files too — links like
<a href="https://your-online-site.com/about">should become<a href="<?php echo site_url('about'); ?>">
.htaccess File (If You’re Using One) A misconfigured .htaccess can force redirects to the live site without you realizing it.
- Open the
.htaccessfile in your project’s root directory - Look for any lines that contain your live URL, like
RewriteRule ^(.*)$ https://your-online-site.com/$1 [R=301,L]— delete or comment these out - Make sure your
.htaccessuses the standard CodeIgniter rewrite rules for local environments:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
Browsers love caching 301 redirects, so even after fixing your code, it might still redirect to the live site.
- Open your browser’s developer tools (press F12)
- Go to the Network tab and check the box for Disable cache (keep the dev tools open while refreshing)
- Alternatively, clear your browser’s entire cache and delete cookies associated with your live site
Some sites store the base URL in the database (e.g., in a settings table).
- If you imported the live database to your local setup, open your database manager (phpMyAdmin, etc.)
- Look for a table that holds site configuration (common names:
settings,system_config) - Find the field for
site_urlorbase_urland update it to your local URL (e.g.,http://localhost)
If you’re using XAMPP, WAMP, or LAMP, double-check your server settings:
- For Apache, open
httpd-vhosts.confand make sure your local site’s virtual host doesn’t have aRedirectdirective pointing to the live site - Check your system’s
hostsfile (Windows:C:\Windows\System32\drivers\etc\hosts; macOS/Linux:/etc/hosts) to ensurelocalhostisn’t mapped to your live site’s IP (this is rare, but worth checking)
Once you work through these steps, your local CodeIgniter site should stop redirecting to the live site and load your modified code. If you’re still stuck, do one more global search for your live URL — sometimes hardcoded links hide in unexpected places like email templates or custom helper functions!
内容的提问来源于stack exchange,提问作者Haris Saif

