Php

如何進行 Apache 301 重定向以保留推薦人?

  • July 26, 2018

我的 httpd.conf 文件中有一個 Apache 301 重定向,它將所有流量重定向到單個域以進行規範化。我最近注意到此重定向不會在標頭中保留引薦來源網址。 RFC2616似乎表明保留引薦來源網址在重定向時是可選的,實際上在 HTTP 和 HTTPS 之間交叉時建議不要這樣做。

不過,我發現了其他問題,其中簡單的 Apache 301 重定向保留了引薦來源網址,預設情況下通常是這種情況。還有哪些其他因素可能會影響這種行為,我可以利用這些因素來保留推薦人?唯一的因素是基於 Apache 伺服器版本和/或客戶端的瀏覽器設置(IE。我無法控制)嗎?

我的重定向:

ServerAlias www.example.com sub1.example.com   

## Redirect all sub-domains to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]

關於上述重寫規則,我正在訪問http://sub1.example.com. 為了簡單起見,我目前只是使用 HTTP,直到我弄清楚這一點。

我確定引薦來源網址沒有被轉移的方法是通過打開開發者工具的瀏覽器手動訪問重定向的站點。我還確定在$_SERVER接收頁面上的 PHP 全域中不包含引薦來源網址。我可以確認只有 1 個重定向發生。伺服器端語言是 PHP。

這是初始頁面 (sub1.example.com) 的請求標頭:

GET / HTTP/1.1
Host: sub1.example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

這是初始頁面 (sub1.example.com) 的響應標頭:

HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Oct 2017 18:14:34 GMT
Server: Apache
Upgrade-Insecure-Requests: 0
Location: http://www.example.com/
Cache-Control: max-age=1
Expires: Wed, 04 Oct 2017 18:14:35 GMT
Content-Length: 243
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

這是接收頁面 (www.example.com) 的請求標頭:

GET / HTTP/1.1
Host: www.example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: <redacted>

接收頁面 (www.example.com) 的響應標頭:

HTTP/1.1 200 OK
Date: Wed, 04 Oct 2017 18:14:34 GMT
Server: Apache
Upgrade-Insecure-Requests: 0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

重定向,301 或其他,將響應發送回客戶端(使用者的瀏覽器),它只是說“再次發送您的請求,但這次使用foo.com.”。如果客戶端選擇再次發送,那麼它會發送,並且它如何制定請求完全取決於客戶端。由於伺服器端沒有任何操作,因此您無法影響引用者的包含。

引用自:https://serverfault.com/questions/876655