在codeigniter中将每个请求从https重定向到http

时间:2015-06-25 12:27:42

标签: .htaccess codeigniter

我的htaccess看起来像:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

如果用户打开https://example.com之类的网址。它应该重定向到http://example.com

但现在当我打开像https://example.com这样的网址时,它会提供此网页不可用

我又尝试了一个代码,将 https 重定向到config.php中的 http

if($_SERVER['HTTPS']) 
{  
        $host = $_SERVER['HTTP_HOST']; 
        $request_uri = $_SERVER['REQUEST_URI']; 
        $good_url = "http://" . $host . $request_uri; 
        $config['base_url'] = $good_url;
        header( "HTTP/1.1 301 Moved Permanently" ); 
        header( "Location: $good_url" ); 
        exit; 
}

它们都不起作用。

1 个答案:

答案 0 :(得分:2)

您的.htaccess代码不正确。

您使用的代码(如下所示)说:“如果启用了HTTPS,则会重定向到HTTPS版本”,这不是您想要的。

RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

此外,这些规则位于文件的底部,而不是顶部。

要解决此问题,请改用以下.htaccess

RewriteEngine on

# Turn off HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Send everything else to the CI front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

请务必删除您添加到config.php的代码 - 这不是必需的。