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

Php 如何在codeigniter中将http重定向到https,php,.htaccess,codeigniter,Php,.htaccess,Codeigniter,第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/in

第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] 
现在我有了解决办法, 我更新了我的htaccess文件--


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

请检查这是否有帮助

配置更改:-转到“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());
    }
}
是的,的确如此

   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
文件替换为
https


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

,对我来说,上面的答案都不起作用,因为它显示了太多的重定向,但这一个很有魅力。因此,我想分享一下,如果其他人也卷入了类似的问题:

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]

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

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


在@asiya khan的回答中,首先转到application/config/config.php文件并查找以下行:

$config['base_url'] = "";
使用https将其设置为您的URL,例如
https://example.com

$config['base_url'] = "https://example.com";
然后转到
.htaccess
文件,在下面给出的示例中添加这一行

重写规则^(.*)$$1[R,L]


#选项+FollowSymLinks
#选项-索引
重新启动发动机
#通过index.php发送请求
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]
重写规则^(.*)$https://example.com/$1[R,L]

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

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]

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

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


这对我来说很有用,你可以在你的.htaccess中输入,只需在最后一行输入即可

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

将此文件插入.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>

重新启动发动机
重写条件%{HTTPS}关闭
重写规则。*https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
重写cond%{HTTP_HOST}^www\。[北卡罗来纳州]
重写规则。*https://www.%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]

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

$config['base_url']
在值中添加“s”


对于Codeigniter 4,遵循以下路径:

/app/config/app.php

将这一行改为true

public $forceGlobalSecureRequests = false;

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

只需在现有规则中添加这一行即可。可能有用

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

设置此
$route['404_override']=''已经设置。但是同样的问题发生了,并将您的htaccess设置为如何在htaccess中设置?我已经在htaccess重写引擎中设置了RewriteCond%{HTTP\u HOST}^myhost\.com$[NC]RewriteRule^(.*)$[R=301,L]RewriteCond%{THE\u请求}^[A-Z]+\/index\.php(/[^\]*)?\HTTP/RewriteRule^index\.php(/(.*)?)$[R=301,L]重写cond%{REQUEST_FILENAME}-f RewriteCond%{REQUEST_FILENAME}-重写了规则^(.*)$index.php/$1[L],但问题是..如果我键入www.myhost.com,那么它将重定向到www.myhost.com。我想重定向。你能给出一些理由说明为什么使用这个php实践比htaccess更好吗?他们每运行一次都会检查一条特定的规则。对于我来说,SSL代码检测不起作用。可能是因为我们使用CloudFlare CDN提供SSL。任何htaccess重写代码都不适合我!最好的方法是使用htaccess方法,因为这是对服务器的第一个调用点。防止任何额外的计算。检查此URL的工作原理您的第一个重写规则不应该有“[L]”@Risteard它给了我一个有趣的错误,在线阅读后我理解了原因,这就是为什么我第一次包含它如果我访问http链接,那么刷新页面时不起作用,那么它将转换为https为什么在第一次访问它时,s未直接转换为https I在基本url中添加了s,但这不会重定向到httpstry清除缓存。请手动删除“应用程序/缓存”文件夹中的缓存项
$config['base_url']
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];
$config['base_url'] = 'https://'.$_SERVER['HTTP_HOST'];
public $forceGlobalSecureRequests = false;
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]