CodeIgniter 3 + XAMPP 环境下无法移除URL中的index.php求助
Hey, I see you've already dug through several posts and tried basic configurations, but that 404 error when skipping index.php is a common snag with subdirectory deployments. Let's work through the fixes one by one:
Update Your
base_urlConfiguration
Your current$config['base_url']points tohttp://localhost, but your site lives in the/mysitesubdirectory. This misalignment throws off CodeIgniter's routing logic. Update it to:$config['base_url'] = "http://localhost/mysite/";Adjust .htaccess with
RewriteBase
Apache needs explicit guidance on the base directory for rewrite rules when working in a subfolder. Modify your .htaccess file (make sure it’s placed in the same folder as CodeIgniter’sindex.php) to includeRewriteBaseand tweak the final rule:RewriteEngine on RewriteBase /mysite/ RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA]The key changes here are adding
RewriteBase /mysite/and removing the?in the RewriteRule—CodeIgniter doesn’t require that query string format for routing.Verify .htaccess Location
Double-check that your .htaccess file is placed directly inhtdocs/mysite/(the same directory asindex.php). If it’s in the roothtdocsfolder or another location, the rewrite rules won’t apply to your site.Enable
AllowOverridein Apache'shttpd.conf
You’ve turned on the rewrite module, but Apache might still be blocking .htaccess rules in your subdirectory. Openhttpd.confand find the<Directory>block for your site’s folder (or the roothtdocsfolder). ChangeAllowOverride NonetoAllowOverride All:<Directory "C:/xampp/htdocs/mysite"> AllowOverride All Require all granted </Directory>Don’t forget to restart Apache after making this change—the configuration won’t take effect until you do.
Try a Different
uri_protocol
Your current settingREQUEST_URIworks for most environments, but switching toAUTOcan help CodeIgniter correctly parse the URL in some cases. Updateconfig.php:$config['uri_protocol'] = 'AUTO';Confirm
mod_rewriteis Actually Enabled
To be 100% sure the rewrite module is active, create a file namedinfo.phpin yourmysitefolder with this content:<?php phpinfo(); ?>Visit
http://localhost/mysite/info.phpand search for "mod_rewrite". If it doesn’t show up, go back tohttpd.confand confirm the lineLoadModule rewrite_module modules/mod_rewrite.sois uncommented, then restart Apache again.
After making all these changes and restarting Apache, test accessing http://localhost/mysite/newpage—it should load without needing index.php now.
内容的提问来源于stack exchange,提问作者Chris S

