簡體   English   中英

多個htaccess使用http重定向到https,但頁面除外

[英]multiple htaccess redirects with http to https but except page

我使用mod_rewrite允許“友好的URL”替換搜索參數,效果很好。 但是,我現在需要將所有流量重定向到https://,但只允許某些頁面保留在http://上(我需要這樣做,以便我的引薦由發送流量的網站收集)

這些年來,我已經添加到.htaccess文件中,大多數都可以,但是我不完全理解它,因此它也可能變得凌亂:-(

我有以下內容(我刪去了我認為不相關的任何內容)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]
</IfModule>

<IfModule mod_rewrite.c>

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

以上工作正常,但將所有頁面重定向到https

我嘗試將/ redirect https重定向部分添加到下面,以允許urlout.php不被重定向,但這只是循環而已,並且我得到一個瀏覽器警告,該頁面有太多重定向:-(

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]

我也嘗試過其他一些事情,但是它們要么不重定向,而所有內容都轉到https,或者我收到循環錯誤或內部500錯誤...

基本上我希望所有頁面都轉到https,但不希望urlout.php

對以上任何代碼的任何建議,我將不勝感激!

回答我自己的問題:

我設法做到了,這是我在第一個條件之后不添加[NC]的一個愚蠢的錯誤...

但是,對於其他人,以下代碼會將所有頁面從http://重定向到https://,除了1頁(在我的情況下為urlout.php)

# redirects any http:// traffic to https:// with exception of urlout.php
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/urlout\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

我需要這樣做,以便我向其發送流量的站點可以將我的域視為“ HTTP引薦來源網址”。

但是,我找到了一種將引薦來源網址從https傳遞到http(盡管特定於瀏覽器!)的簡單方法,將其添加到HTML頭中:

<meta name="referrer" content="origin">

我的參考是(除其他之外): moz.com的Meta Referrer Tag博客文章

如果要將某些特殊頁面重定向到https,例如,要重定向:

-https://www.example.com/page2.html

您可以嘗試以下方法:

 RewriteEngine on

 RewriteCond %{HTTPS} off
 RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]

如果要將整個站點從http重定向到https,可以使用以下命令:

 RewriteEngine on



 RewriteCond %{HTTPS} off
 RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]

RewriteCond%{HTTPS}關閉對於避免重定向循環很重要,因為它跳過了https請求的規則。

暫無
暫無

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

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