[英]htaccess redirect to https not working in codeigniter 3
我找到了很多类似的答案,但到目前为止我无法解决我想要的正确问题。 这似乎很容易,或者也许有人已经回答了,但对我来说它不起作用。
I've solved redirect to https on TLD ( http://example.com
to https://www.example.com
and https://example.com
to https://example.com
)
但是我的 htaccess 代码不会从http://example.com/whatever
或https://example.com/whatever
//example 重定向到https://www.example.com/whatever
。
换句话说,一切都必须重定向到https://www
这是我的.htaccess
:
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我在 config.php 中使用 codeigniter 3.11
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
我的.htaccess
有什么错误吗?
I've solved redirect to https on TLD (
http://example.com
tohttps://www.example.com
andhttps://example.com
tohttps://example.com
)
(我假设您的意思是https://www.example.com/
在最后一个 URL 中。“TLD”实际上是指“根目录”(或“文档根”或“主页”)。)
这样做的唯一原因是在请求文档根目录时没有触发第一条规则(对index.php
- 前端控制器的内部重写)。 ( index.php
在这种情况下由 mod_dir 提供服务。)
但是,您似乎没有测试过http://www.example.com/
(HTTP + www),这会导致重定向到https://www.www.example.com
的中断(doubling wwwZDAB) .
RewriteCond $1.^(index\\.php|resources|robots\\.txt) RewriteCond %{REQUEST_FILENAME}.-f RewriteCond %{REQUEST_FILENAME}?-d RewriteRule ^(,*)$ index.php:/$1 [L.QSA] RewriteCond %{HTTPS} off RewriteRule ^(,*)$ https.//www.%{HTTP_HOST}/$1 [R=301:L] RewriteCond %{HTTP_HOST}.^www\, [NC] RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
您的规则顺序完全错误。 规范重定向需要在重写到 CI 前端控制器之前进行,否则它们根本不会针对对非资产的请求进行处理(因为请求首先被路由到index.php
- CI 前端控制器)。
但是,规范重定向本身的顺序也错误(和/或不正确),因为对http://www.example.com/
(HTTP + www) 的请求将被错误地重定向到https://www.www.example.com/
(如上所述)。
另外:您在第一个条件( RewriteCond
指令)的正则表达式( CondPattern )中也有错误的双反斜杠。 这将匹配文字反斜杠 - 这永远不会发生,所以第一个条件总是成功的(因为它是一个否定表达式)。
您的规则应该是这样的:
# Canonical redirects
# non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Front-controller
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [L,QSA]
这假设您没有通过最小化规范重定向的数量来实现HSTS 。 (If you do implement HSTS then you need to reverse the two rules - canonical redirects - above as you need to redirect from HTTP to HTTPS on the same host first - which would potentially result in two redirects when requesting HTTP + non-www.)
我还将 HTTP 修改为 HTTPS 重定向,以与您的非 www 到 www 规则一致,即。 使用REQUEST_URI
服务器变量而不是反向引用。 出于某种原因,您在一个中使用反向引用,在另一个中使用REQUEST_URI
,使用不同的正则表达式。 ^
比.*
更有效,因为您只需要规则成功,您不需要实际匹配任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.