简体   繁体   English

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

[英]How to redirect http to https in codeigniter

Point #1 If I type:第 1 点如果我输入:

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

have it redirect to让它重定向到

https://www.myurl.com/somepage

Point #2第 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

From Comment htaccess来自评论 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] 

Please Check this may help请检查这可能有帮助

Config changes :- Go to “application/config/config.php” and enable or set hooks to true.配置更改:- 转到“application/config/config.php”并启用或设置挂钩为 true。

$config['enable_hooks'] = TRUE;

create a new file named hooks.php in “application/config/hooks.php” and add below code in hooks.php:-在“application/config/hooks.php”中创建一个名为 hooks.php 的新文件,并在 hooks.php 中添加以下代码:-

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

Now create a new directory with named “hooks” under application directory and then create new file named “ssl.php” in “application/hooks/ssl.php” and add below code to “ssl.php” :-现在在应用程序目录下创建一个名为“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());
    }
}

I tried above all ones but they did not work properly in my case.我首先尝试了所有方法,但在我的情况下它们无法正常工作。 I found below solution, it works in my case.我找到了以下解决方案,它适用于我的情况。 I hope it will be helpful for you as well.我希望它对你也有帮助。

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]

Now I got a solution, I updated my htaccess file.--现在我有了一个解决方案,我更新了我的 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]

Now, It worked for me smoothly.. :)现在,它对我来说很顺利.. :)

Insert this into .htaccess将其插入 .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>

If you, specifically want to redirect, above answers are helpful.如果您特别想重定向,以上答案会有所帮助。 But if you are doing this because base is "HTTP", then,但是如果你这样做是因为 base 是“HTTP”,那么,
redirecting is not correct method (as it asks for resources).重定向不是正确的方法(因为它需要资源)。
Instead, open "application/config/config.php" in your project and change the value of相反,在您的项目中打开“application/config/config.php”并更改

$config['base_url']

Add "s" in the value在值中添加“s”
from

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

to

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

for me none of the above answer worked as it was showing me too many redirects but this one worked like charm.对我来说,上面的答案都没有奏效,因为它向我展示了太多的重定向,但这一个就像魅力一样。 Hence I thought to share if someone else get into the similar issue:因此,如果其他人遇到类似问题,我想分享一下:

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]

First go to the application/config/config.php file and look for this line:首先转到 application/config/config.php 文件并查找这一行:

$config['base_url'] = "";

Set it to your URL with the https eg https://example.com使用 https 将其设置为您的 URL,例如https://example.com

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

Then go to your .htaccess file and add this line in the example given below.然后转到您的.htaccess文件并在下面给出的示例中添加这一行。

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

<IfModule mod_rewrite.c>

    #Options +FollowSymLinks
    #Options -Indexes
    RewriteEngine on

    # 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>

This work for me you can input it in your .htaccess just input at the last line这对我有用,你可以在你的 .htaccess 中输入它,只需在最后一行输入

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

For Codeigniter 4, follow the path;对于 Codeigniter 4,遵循路径;

/app/config/app.php /app/config/app.php

and change this line to true.并将此行更改为 true。

public $forceGlobalSecureRequests = false;

it will redirect to https directly.它将直接重定向到 https。 no hook, no .htaccess.没有钩子,没有 .htaccess。

yes indeed this是的,确实这个

   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]

works but you have to make a slight edit.工作,但你必须做一个轻微的编辑。 In the codeiginiterfolder/application/config.php file replace http with https .codeiginiterfolder/application/config.php文件中,将http替换为https

Also, the base url in the above code you have to replace the myhost to your host name.此外,上述代码中的基本 url 您必须将myhost替换为您的主机名。 Suppose my host name internetseekho.com so instead of myhost you do have write internetseekho .假设我的主机名为internetseekho.com因此您确实写了internetseekho而不是myhost

If you find the error "No Input file specified" then add "?"如果您发现错误“未指定输入文件”,则添加“?” in the last line在最后一行

RewriteRule ^(.*)$ index.php/?$1 [L]重写规则 ^(.*)$ index.php/?$1 [L]

in the answer from @asiya khan@asiya khan的回答中

Our SSL is from the CloudFlare CDN and so the solutions above do not work.我们的 SSL 来自 CloudFlare CDN,因此上述解决方案不起作用。

The following SSL detection code (in SSL.php based on @Preetham Hegde solution) works:以下 SSL 检测代码(在基于 @Preetham Hegde 解决方案的 SSL.php 中)有效:

$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();
}

Just add this line in your existing rules.只需在现有规则中添加这一行。 It may work!它可能会起作用!

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM