删除';后,域后需要额外的斜杠;index.php';来自Codeigniter


Extra slash after domain required after removing 'index.php' from Codeigniter

我一直在尝试使用mod重写从我的Codeigniter站点中删除"index.php"。然而,我最终遇到了这个问题:

原始URI:http://mysite.com/index.php/about

在删除index.php后,使其工作的唯一方法:http://mysite.com//about(注意多余的斜线)

删除额外的斜杠会导致Codeigniter无法"查看"域后的url字符串。

我现在已经尝试了3种不同的mod重写方法,但最终都遇到了相同的问题——除非在域名后添加一个额外的斜杠,否则URI是不起作用的。

我当前的mod_rewrite代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index'.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index'.php/*(.*) /$1$2 [R=301,L]
RewriteCond $1 !'.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co
RewriteRule (.*) http://domainname.co/$1 [R=301,L]
</IfModule>

首先:请发布您正在使用的mod_rewrite代码,以确保其正确。

第二:在您的应用程序配置文件中检查您的索引页面是否设置为与此$config['index_page'] = ''; 完全不同

这个问题的解决方案非常简单。

像这样编辑你的.htaccess-

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<directory_where_your_code_is_kept>/
RewriteCond %{THE_REQUEST} ^GET.*index'.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index'.php/*(.*) /$1$2 [R=301,L]
RewriteCond $1 !'.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co
RewriteRule (.*) http://domainname.co/$1 [R=301,L]
</IfModule>

然后转到config.php并进行这些更改-

$config['base_url'] = 'http://localhost/<directory_where_your_code_is_kept>';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';