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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34586278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:06:07  来源:igfitidea点击:

How to redirect http to https in codeigniter

php.htaccesscodeigniter

提问by asiya khan

Point #1If 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/ [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/ [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/ [L] 

采纳答案by asiya khan

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/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]

Now, It worked for me smoothly.. :)

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

回答by Preetham Hegde

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

回答by Nioya

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

回答by Harshal Lonare

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

回答by Naijajeodax

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}

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

回答by Rodrigo

Poner en el .htaccess entre los . Al menos a mi me funciona así. Saludos

Poner en el .htaccess entre los . Al menos a mi me funciona así。萨卢多斯

<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/ [L]

</IfModule>

回答by hitesh balyan

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的回答中

回答by Kwame Oteng Appiah-Nti

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 e.g. https://example.com

使用 https 将其设置为您的 URL,例如 https://example.com

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

Then go to your .htaccessfile 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/ [L]
    RewriteRule ^(.*)$ https://example.com/ [R,L]


</IfModule>

回答by James

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

回答by interntseekho

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/ [R=301,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/ [L]

works but you have to make a slight edit. In the codeiginiterfolder/application/config.phpfile replace httpwith https.

工作,但你必须做一个轻微的编辑。在 codeiginiterfolder/application/config.php文件中替换httphttps.

Also, the base url in the above code you have to replace the myhostto your host name. Suppose my host name internetseekho.comso instead of myhostyou do have write internetseekho.

此外,您必须将上述代码中的基本 url 替换myhost为您的主机名。假设我的主机名internetseekho.com而不是myhost你有 write internetseekho