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

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:

1. Update the Base URL in 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.php in your local project
  • Search for the line $config['base_url'] — it’ll probably be set to your live site’s full URL (like https://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), use http://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'] = '';
2. Hunt for Hardcoded Redirects

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 using site_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'); ?>">
3. Fix Your .htaccess File (If You’re Using One)

A misconfigured .htaccess can force redirects to the live site without you realizing it.

  • Open the .htaccess file 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 .htaccess uses the standard CodeIgniter rewrite rules for local environments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
4. Clear Browser Cache and Cookies

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
5. Check for Database-Stored Site URLs

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_url or base_url and update it to your local URL (e.g., http://localhost)
6. Verify Local Server Configuration

If you’re using XAMPP, WAMP, or LAMP, double-check your server settings:

  • For Apache, open httpd-vhosts.conf and make sure your local site’s virtual host doesn’t have a Redirect directive pointing to the live site
  • Check your system’s hosts file (Windows: C:\Windows\System32\drivers\etc\hosts; macOS/Linux: /etc/hosts) to ensure localhost isn’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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:37:29