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

CodeIgniter中通过.htaccess移除URL中的index.php失败求助

Troubleshooting CodeIgniter .htaccess to Remove index.php

Hey there! Let's work through why your .htaccess isn't stripping index.php from your CodeIgniter URLs—I've dealt with this exact frustration before, so here's what to check and adjust:

  • First, confirm Apache's mod_rewrite is enabled
    A super common oversight: make sure the rewrite module is turned on. For local servers like XAMPP/WAMP, open your Apache httpd.conf file, find the line #LoadModule rewrite_module modules/mod_rewrite.so, and delete the # to uncomment it. Then restart Apache to apply the change.

  • Verify your .htaccess location
    The file needs to live in your CodeIgniter root directory—the same folder that holds your application and system folders. If it's tucked inside a subfolder like application, it won't apply to your entire site.

  • Update your .htaccess rules
    Your current rules might need a tweak depending on your server setup. Try replacing your existing code with this:

    RewriteEngine On
    RewriteBase /your-project-folder/  # Replace this with your actual base path (use / if it's your root domain)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    If that doesn't work, try this variation (some shared hosts require this syntax):

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    The key difference is the ? after index.php—it helps some servers correctly pass the URI to CodeIgniter.

  • Fix your CodeIgniter config settings
    Don't skip these critical steps in application/config/config.php:

    1. Set $config['index_page'] = ''; (empty string—this tells CodeIgniter not to append index.php to URLs)
    2. Ensure $config['base_url'] is set correctly without index.php at the end. For example:
      $config['base_url'] = 'http://localhost/your-project/';
      
  • Test with a simple route
    Create a basic controller (like the default Welcome.php with an index method) and try accessing http://your-domain/welcome instead of http://your-domain/index.php/welcome to see if the rewrite works.

If none of this fixes it, check your Apache virtual host or httpd.conf file—make sure the AllowOverride All directive is set for your project directory. If it's set to None, your .htaccess rules will be ignored entirely.

内容的提问来源于stack exchange,提问作者Joby Mathews

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:40:39