【问题标题】:How to redirect http to https in codeigniter如何在codeigniter中将http重定向到https
【发布时间】:2016-04-07 18:40:26
【问题描述】:

第 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] 

【问题讨论】:

  • 设置这个$route['404_override'] = '';
  • 已经设置。但是同样的问题发生了
  • 也设置你的htaccess
  • 如何在htaccess中设置?
  • 我已经在 htaccess RewriteEngine On RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC] RewriteRule ^(.*)$ myhost.com/$1 [R=301,L] RewriteCond %{THE_REQUEST } ^[AZ]+\ /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] 但问题是..如果我输入 www.myhost.com 然后它重定向到 www .myhost.com。我想重定向myhost.com

标签: php .htaccess codeigniter


【解决方案1】:

只需在现有规则中添加这一行。它可能会起作用!

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

【讨论】:

    【解决方案2】:

    对于 Codeigniter 4,遵循路径;

    /app/config/app.php

    并将这一行更改为 true。

    public $forceGlobalSecureRequests = false;
    

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

    【讨论】:

      【解决方案3】:

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

      $config['base_url']
      

      在值中添加“s”
      来自

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

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

      【讨论】:

      • 我在 base_url 中添加了 s 但这,domaindotcom 没有重定向到 https
      • 手动删除“application/cache”文件夹中的缓存项
      【解决方案4】:

      这项工作对我来说你可以在你的 .htaccess 中输入它,只需在最后一行输入

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

      【讨论】:

        【解决方案5】:

        将其插入 .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>
        

        【讨论】:

          【解决方案6】:

          我们的 SSL 来自 CloudFlare CDN,因此上述解决方案不起作用。

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

          【讨论】:

            【解决方案7】:

            我首先尝试了所有这些,但在我的情况下它们不能正常工作。我找到了以下解决方案,它适用于我的情况。希望对你也有帮助。

            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]
            

            【讨论】:

              【解决方案8】:

              确实如此

                 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,而是internetseekho

              【讨论】:

                【解决方案9】:

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

                $config['base_url'] = "";
                

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

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

                然后转到您的.htaccess 文件并在下面给出的示例中添加这一行。

                重写规则 ^(.*)$ 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>
                

                【讨论】:

                • 你的第一个重写规则不应该有“[L]”
                • @Risteard 它给了我一个有趣的错误,在网上看了一点之后我明白了为什么,这就是我包含它的原因
                【解决方案10】:

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

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

                @asiya khan

                的回答中

                【讨论】:

                  【解决方案11】:

                  对我来说,上述答案都不起作用,因为它向我展示了太多的重定向,但这个答案就像魅力一样。因此,如果其他人遇到类似问题,我想分享一下:

                  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]
                  

                  【讨论】:

                    【解决方案12】:

                    现在我有一个解决方案, 我更新了我的 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]
                    

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

                    【讨论】:

                    • 第一次如果我使用http访问链接所以当我刷新页面时它不起作用然后它转换为https为什么在第一次访问它时不会直接转换为https
                    【解决方案13】:

                    请检查这可能会有所帮助

                    配置更改:- 转到“application/config/config.php”并启用或将挂钩设置为 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());
                        }
                    }
                    

                    【讨论】:

                    • 你能给出一些论据,为什么使用这个 php 实践是比 htaccess 更好的方法?他们每次运行都会检查某个规则
                    • 对我来说,SSL 代码检测不起作用。可能是因为我们使用 CloudFlare CDN 来提供 SSL。
                    • 任何 htaccess 重写代码都不适合我!钩子工作
                    • 最好的方法是使用 htaccess 方法,因为这是调用服务器的第一个点。防止任何额外的计算。检查此 URL 的工作原理danielmorell.com/images/articles/htaccess_guide/…
                    猜你喜欢
                    • 2017-12-13
                    • 2018-12-26
                    • 2016-04-29
                    • 2015-04-23
                    • 2010-09-05
                    • 1970-01-01
                    • 2019-12-23
                    • 2018-05-17
                    相关资源
                    最近更新 更多