簡體   English   中英

htaccess 重定向到 https 在 codeigniter 中不起作用 3

[英]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/whateverhttps://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 to https://www.example.com and https://example.com to https://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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM