求助:CodeIgniter应用部署后出现307重定向问题的解决办法
Hey there, let's work through this 307 redirect problem you're dealing with after moving your CodeIgniter folders into the public directory under public_html. I've debugged similar hosting setup issues before, so here are the key fixes to apply:
1. Update the Entry Point (index.php) Paths
The most common culprit here is that your main index.php file can't find the application and system folders anymore. If you moved those folders into public, you need to adjust the paths in index.php (which should be in your public_html root, or inside public—depending on your setup):
Open your index.php file and modify these lines:
// Path to the application folder $application_folder = './public/application'; // Path to the system folder $system_path = './public/system';
Make sure the paths match where you actually moved the folders. If index.php is inside the public folder itself, you'd use ./application and ./system instead.
2. Correct config.php Settings
Next, head to application/config/config.php and double-check these critical settings:
- Base URL: Set
$config['base_url']to your actual domain, including the correct path ifpublicis part of the URL. For example:
If you want the$config['base_url'] = 'https://yourdomain.com/public/';publicfolder to be hidden (so your URL is justhttps://yourdomain.com/), you'll need to tweak your.htaccessfile (more on that below). - Index Page: Ensure
$config['index_page']is set correctly. If you're using.htaccessto removeindex.php, set it to an empty string:$config['index_page'] = '';
3. Fix .htaccess Rules
A misconfigured .htaccess is often the cause of 307 redirect loops. Here's a working rule set tailored to your folder structure:
If your index.php is in public_html/public, create or update the .htaccess in public_html (the root) to redirect all traffic to the public folder:
RewriteEngine On RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(.*)$ /public/$1 [L,R=301]
Then, inside the public folder, add another .htaccess to handle CodeIgniter's clean URLs:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If your index.php is already in the public_html root and you moved the core folders into public, adjust the .htaccess to point to the correct index.php path.
4. Check Server Directory Permissions
Sometimes 307 redirects can happen if the server doesn't have proper access to your application or public folders. Make sure:
- The
applicationfolder has permissions set to755(read/execute for everyone, write for owner) - The
publicfolder also uses755permissions - Files inside these folders should be
644(read/write for owner, read for others)
5. Verify Default Route and Controller
If the redirect is happening when accessing the root URL, check your application/config/routes.php to ensure the default route points to an existing controller:
$route['default_controller'] = 'Home'; // Make sure Home.php exists in controllers folder
If the controller file name or class name is misspelled (CodeIgniter is case-sensitive in some setups), this can trigger unexpected redirects.
After making these changes, clear your browser cache and test the site again. If the 307 issue persists, check your server's error logs—they often provide specific details about what's causing the redirect loop.
内容的提问来源于stack exchange,提问作者Kanthareddy Venkat

