簡體   English   中英

如何將 http 重定向到 https 加上重定向`index.html` 到另一個 html 頁面在 Z8052C47AB3B4ADCCC2A 一起?

[英]how to redirect http to https plus redirect `index.html` to another html page in .htaccess together?

I have a code in .htaccess that redirects http to https but I also want to add a code that redirects the index.html page to another html page. 有沒有辦法在.htaccess中同時做這兩個。 下面提供的代碼。

http 轉 https 代碼

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

index.html 到另一個 html 頁面代碼

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]
 RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^$ http://example.com/index.php [L,R=301]

是的,這是一個外部重定向,從http(s)://example.com/重定向到http://example.com/index.php However, you are redirecting to HTTP, not HTTPS and you are redirecting to index.php , whereas you stated index.html in your question. 似乎不需要檢查請求的主機名,除非您有多個域並且只想重定向一個實例。

Unless you plan to implement HSTS then include this redirect before your general HTTP to HTTPS redirect and include the canonical hostname in your HTTP to HTTPS redirect.

例如:

RewriteEngine On

# Redirect / or /index.html TO /dir/index.html (HTTPS)
RewriteRule ^(index\.html)?$ https://example.com/dir/index.html [R=302,L]

# Redirect HTTP to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]

但是,在閱讀了您的鏈接問題后,外部“重定向”可能不是您問題的正確解決方案。 當您應該鏈接到子目錄中的文件時,聽起來您可能“錯誤地”鏈接到文檔根目錄中的文件? (真的,如果是這種情況,您應該更正您的內部鏈接......)但是,一個可行的解決方案可能是在內部將請求重寫為所需的 URL 路徑,或者如果您要鏈接到文檔根目錄,則更改DirectoryIndex . 這對用戶/客戶端完全隱藏 - 用戶只看到 URL A,但您實際上是在為 URL B 提供服務。但這取決於您網站的結構。

An external redirect, on the other hand, physically sends the user/browser to URL B. They see both URL A (the link they click on) and URL B (the URL they are being redirected to) - as does a search engine bot . 瀏覽器需要發出兩個請求。 這對用戶來說可能會更慢,並且在您的服務器上放置更多工作(不多,但仍然更多)。

你想先做你的 http --> https 因為這既適用於原始通信又適用於安全通信的重定向。

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    Header always set Content-Security-Policy "upgrade-insecure-requests;"

現在您可以將重定向應用到index.php

     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteRule ^index\.php$ - [L]
     RewriteCond %{REQUEST_FILENAME} -f
     RewriteCond %{REQUEST_FILENAME} -d
     RewriteRule . /index.php [L]
     </IfModule>

您可以調整-f-d以包含! 和/或更改文件名類型以滿足您的特定需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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