如何去除CodeIgniter网站admin路径中的index.php
Hey there, let's figure out why your /admin route is still redirecting to the version with index.php while other pages work fine. I've run into this exact issue a couple times, so here are some targeted fixes to try:
1. Double-check your root .htaccess rules
First, make sure your main .htaccess file in the website root doesn't have any rules that are excluding the /admin path. A correct, standard CodeIgniter .htaccess should look like this:
RewriteEngine On # Disable directory browsing Options -Indexes # Allow any files or directories that exist to be displayed directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite all other URLs to index.php/URL RewriteRule ^(.*)$ index.php/$1 [L]
Also, confirm that your Apache server has AllowOverride All enabled for your site's directory (in your virtual host or httpd.conf file) — if this is set to None, your .htaccess rules won't apply at all.
2. Verify CodeIgniter config settings
Head over to application/config/config.php and check these key settings:
$config['index_page']: Make sure this is set to an empty string:$config['index_page'] = '';$config['base_url']: Set this to your exact base URL withoutindex.php, e.g.,$config['base_url'] = 'https://mywebsite.com/';(match whether you use www or not consistently)$config['uri_protocol']: Try setting this to'REQUEST_URI'if it's currently set to something else. Sometimes'AUTO'can cause routing quirks with certain server setups.
3. Check for conflicting route rules
Take a look at application/config/routes.php to make sure there's no incorrect route for your admin controller. Avoid any rules that explicitly include index.php, like:
// ❌ Wrong $route['admin'] = 'index.php/admin'; // ✅ Correct (if your admin controller is named Admin.php) $route['admin'] = 'admin';
If you're using a subfolder for admin controllers, make sure your route reflects that properly (e.g., $route['admin'] = 'admin/admin_dashboard'; if your controller is in application/controllers/admin/Admin_dashboard.php).
4. Fix virtual host redirect conflicts
If your server has a rule to force www (or vice versa), make sure that rule runs before your CodeIgniter rewrite rules. For example, if you have a 301 redirect to add www, your .htaccess should structure it like this:
RewriteEngine On # Force www first RewriteCond %{HTTP_HOST} ^mywebsite.com [NC] RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301] # Then CodeIgniter rewrite rules RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If the order is reversed, the server might first apply the CodeIgniter rule (adding index.php) and then redirect to www, resulting in the unwanted URL.
5. Clear browser cache and test in incognito mode
Old redirects can get cached in your browser, so even after fixing the issue, you might still see the old behavior. Test your /admin route in an incognito window or clear your browser's cache and cookies to make sure you're seeing the fresh result.
内容的提问来源于stack exchange,提问作者Shantanu

