如何在codeigniter中将http重定向到https

问题描述 投票:0回答:14

第1点 如果我输入:

www.myurl.com/somepage
http://www.myurl.com/somepage
http://myurl.com/somepage
https://myurl.com/somepage

将其重定向到

https://www.myurl.com/somepage

第2点

When I type in something like www.myurl.com it is redirecting to https://www.myurl.com/index.php.
Make it so the index.php is not displaying. It should just display https://www.myurl.com

来自评论htaccess

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC] 
RewriteRule ^(.*)$ myhost.com/$1 [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 
apache .htaccess codeigniter http-redirect mod-rewrite
14个回答
27
投票

我尝试了以上所有方法,但它们在我的情况下无法正常工作。我找到了以下解决方案,它适用于我的情况。希望对您也有帮助。

RewriteEngine on

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

RewriteCond $1 !^(index\.php|resources|robots\.txt|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

25
投票

请检查这可能有帮助

配置更改:- 转到“application/config/config.php”并启用或将 hooks 设置为 true。

$config['enable_hooks'] = TRUE;

在“application/config/hooks.php”中创建一个名为hooks.php的新文件,并在hooks.php中添加以下代码:-

$hook['post_controller_constructor'][] = array(
                                'function' => 'redirect_ssl',
                                'filename' => 'ssl.php',
                                'filepath' => 'hooks'
                                );

现在在应用程序目录下创建一个名为“hooks”的新目录,然后在“application/hooks/ssl.php”中创建名为“ssl.php”的新文件 并将以下代码添加到“ssl.php”:-

function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('client');  // add more controller name to exclude ssl.
    if(!in_array($class,$exclude)) {
      // redirecting to ssl.
      $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
      if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
    } 
    else {
      // redirecting with no ssl.
      $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
      if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
    }
}

11
投票

现在我找到了解决方案, 我更新了我的 htaccess 文件。--

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

现在,它对我来说很顺利..:)


7
投票

将其插入.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

5
投票

对于 Codeigniter 4,请遵循以下路径;

/app/config/app.php

并将此行更改为 true。

public $forceGlobalSecureRequests = false;

它将直接重定向到 https。没有钩子,没有.htaccess。


4
投票

如果您特别想要重定向,以上答案很有帮助。 但如果你这样做是因为基础是“HTTP”,那么,
重定向不是正确的方法(因为它需要资源)。
相反,在您的项目中打开“application/config/config.php”并更改

的值
$config['base_url']

在值中添加“s”
来自

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];

$config['base_url'] = 'https://'.$_SERVER['HTTP_HOST'];

3
投票

对我来说,上面的答案都不起作用,因为它向我展示了太多的重定向,但这个答案很有魅力。因此,我想分享是否其他人遇到类似的问题:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (sale|success|cancel) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(static|sale|success|cancel) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

1
投票

这对我有用,你可以在 .htaccess 中输入它,只需在最后一行输入

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

1
投票

首先进入 application/config/config.php 文件并查找这一行:

$config['base_url'] = "";

将其设置为带有 https 的 URL,例如

https://example.com

$config['base_url'] = "https://example.com";

然后转到您的

.htaccess
文件并在下面给出的示例中添加此行。

RewriteRule ^(.*)$ https://example.com/$1 [R,L]

<IfModule mod_rewrite.c>

    #Options +FollowSymLinks
    #Options -Indexes
    RewriteEngine on

    RewriteCond %{HTTPS} off 
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    # Send request via index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteRule ^(.*)$ https://example.com/$1 [R,L]
    

</IfModule>

0
投票

如果发现错误“未指定输入文件”,则添加“?” 在最后一行

RewriteRule ^(.*)$index.php/?$1 [L]

来自@asiya khan

的回答

0
投票

确实是这个

   RewriteEngine On
   RewriteCond %{HTTPS} off [OR] 
   RewriteCond %{HTTP_HOST} !^www\. [OR]
   RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
   RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
   RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
   RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]

可以工作,但你必须做一些轻微的编辑。在里面

codeiginiterfolder/application/config.php
文件将
http
替换为
https

此外,您必须将上述代码中的基本 url 替换为您的主机名。假设我的主机名是

myhost
,那么你应该写
internetseekho.com
,而不是
myhost
    


0
投票

以下 SSL 检测代码(在基于 @Preetham Hegde 解决方案的 SSL.php 中)有效:

internetseekho



0
投票

$isSecure = false; if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $isSecure = true; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') { $isSecure = true; } $REQUEST_PROTOCOL = $isSecure ? 'https' : 'http'; if ($REQUEST_PROTOCOL=="http") { $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $redirect); exit(); }



0
投票

对于 Codeigniter 4,请遵循以下路径;

/app/config/app.php

并将此行更改为 true。

公共$forceGlobalSecureRequests = false; 它会直接重定向到 https。没有钩子,没有.htaccess。

© www.soinside.com 2019 - 2024. All rights reserved.