更改为 $config['index_page'] 是正确的,但您还需要做 1 件事:
您必须在您的 codeigniter 根目录中使用 .htaccess 文件,该文件将所有对 index.php 文件的调用重新命名,而无需在 url 栏中包含 index.php
我的文件如下所示:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.YOURSITE.COM/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://YOURSITE.COM/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
这个文件还重新定义了所有非 www 调用(比如 YOURSITE.com 到 www.YOURSITE.com。你也可以这样做,然后你必须注释部分(使用 # )并取消它下面的部分.
它还使 URL 更好(规范化 URL)。如果您查看以下 URL:
/category
/category/index
/category/index/
这是同一控制器中相同功能的 3 个 url。使用上面提供的.htaccess,网址将是
/category
另一件需要注意的重要事项:对于带有index 方法且采用任何类型参数的控制器,这些 URL 仍然可以这样工作:
/welcome/index/123
/category/index/123