Codeigniter 如何强制自动更正http://example.com 到https://example.com 通过.htacess文件
我有一个安装了通配符SSL的域。我们对子域进行了修改,并将子域从HTTP自动重选为HTTPS 但是对于主域,由于它的存在,我无法将它重定向到[没有www的情况下保持不变] 我从网络和研究中得到的所有例子都是针对www到非www或非www到www的 我的要求是非www到非www的HTTPS 适用于我的子域的代码不适用于非WWW 子域上的工作代码如下所示:Codeigniter 如何强制自动更正http://example.com 到https://example.com 通过.htacess文件,codeigniter,url,redirect,https,Codeigniter,Url,Redirect,Https,我有一个安装了通配符SSL的域。我们对子域进行了修改,并将子域从HTTP自动重选为HTTPS 但是对于主域,由于它的存在,我无法将它重定向到[没有www的情况下保持不变] 我从网络和研究中得到的所有例子都是针对www到非www或非www到www的 我的要求是非www到非www的HTTPS 适用于我的子域的代码不适用于非WWW 子域上的工作代码如下所示: RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{HTTP_HOST}
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
我希望强制自动修复来尝试这个。希望对你有用
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
试试这个。希望对你有用
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
从config.php并启用或设置挂钩为true:
$config['enable_hooks'] = TRUE;
然后在config文件夹中创建一个名为hooks.php的新文件,并添加以下内容:
$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
在应用程序文件夹(即application/hooks)中创建一个名为hooks的新目录,然后在其中创建一个名为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());
}
}
从config.php并启用或设置挂钩为true:
$config['enable_hooks'] = TRUE;
然后在config文件夹中创建一个名为hooks.php的新文件,并添加以下内容:
$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
在应用程序文件夹(即application/hooks)中创建一个名为hooks的新目录,然后在其中创建一个名为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());
}
}
嗨,Lalit,试过这个,但对我不起作用。我的服务器在Godaddy Linux主机上。如果此信息有用。@GauravMody请在RewriteCond%{HTTPS}上尝试
Options+FollowSymLinks RewriteEngine=在RewriteCond%{HTTP\u USER\u AGENT}^(.+)$RewriteCond%{SERVER\u NAME}^coolexample\.com$[或]RewriteCond%{SERVER\u NAME}^www\.coolexample\.com$RewriteRule.*https://%{SERVER\u NAME}%{REQUEST\u URI}[R=301,L]头添加严格的传输安全性“max age=300”
Hi Lalit尝试过这个,但对我不起作用。我的服务器在Godaddy Linux主机上。如果此信息有用。@GauravMody请在RewriteCond%{HTTPS}上尝试Options+FollowSymLinks RewriteEngine=在RewriteCond%{HTTP\u USER\u AGENT}^(.+)$RewriteCond%{SERVER\u NAME}^coolexample\.com$[或]RewriteCond%{SERVER\u NAME}^www\.coolexample\.com$RewriteRule.*https://%{SERVER\u NAME}%{REQUEST\u URI}[R=301,L]头添加严格的传输安全性“max age=300”