【问题标题】:Extra slash after domain required after removing 'index.php' from Codeigniter从 Codeigniter 中删除“index.php”后,域后需要额外的斜杠
【发布时间】:2012-08-27 12:33:43
【问题描述】:

我一直在尝试使用 mod-rewrite 从我的 Codeigniter 站点中删除“index.php”。但是我最终遇到了这个问题:

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

只有在删除 index.php 之后才能使其工作http://mysite.com//about(注意多余的斜线)

删除多余的斜杠会导致 Codeigniter 无法“看到”域后的 url 字符串。

我现在尝试了 3 种不同的 mod-rewrite 方法,但最终都遇到了同样的问题 - 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>

【问题讨论】:

    标签: php codeigniter mod-rewrite url-rewriting


    【解决方案1】:

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

    第二:检查您的应用程序配置文件,您的索引页面设置为类似$config['index_page'] = '';

    【讨论】:

    • 谢谢,更新了当前的 mod_rewrite 代码。还在 Codeigniter 论坛上尝试了一个更简单的变体——同样的问题。 RE:$config['index_page'] - 我试过 'index.php' 'index.php/' '' 和 ' '。如果您使用 mod_rewrite,文档建议删除它。
    【解决方案2】:

    问题的解决方法很简单。

    像这样编辑你的.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';
    

    【讨论】:

    • 感谢您的回复,但仍然无法正常工作。你把“RewriteBase //”放在哪里应该是“RewriteBase /”还是“RewriteBase //”? (我的代码在网站的顶级目录中)
    • 没有//。只有一个/。如果它在顶级目录中,只需将其保留为RewriteBase /
    • 好的,就是这样。回复:您的其他建议更改,与我原来的配置唯一不同的是 'uri_protocol' 是 AUTO 而不是 REQUEST_URI。任何其他想法可能导致此问题?谢谢。
    • 应该进行的更改是 - 1. .htaccess 2. config.php中的更改
    • @Zaph 您是在 Linux 环境中还是通过 Wamp 运行它。如果你在 Linux 上运行,那么你需要编辑你的 apache.conf 并为你的目录创建AllowOverride All
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2017-06-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多