Apache:根據主機名稱載入腳本

Apache:根據主機名稱載入腳本

我認為這並不那麼複雜,但我很難找到最好的解決方法。

我們有一個網站,site.example.com下面有 5 個頁面。假設這 5 頁是site.othersite.com/page1.html, page2.html, page3.html, page4.html, page5.html.現在,我們還有其他 5 個子網域,lp1.OTHERSITE.com, lp2.othersite.com, lp3.othersite.com, lp4.othersite.com, lp5.othersite.com.所有這些網域都託管在同一台伺服器上,位於同一文檔根目錄下。現在,我們需要 Apache 做的是對以下模式進行透明重定向(即網址列不變):

lp1.othersite.com --> site.example.com/page1.html
lp2.othersite.com --> site.example.com/page2.html
lp3.othersite.com --> site.example.com/page3.html
lp4.othersite.com --> site.example.com/page4.html
lp5.othersite.com --> site.example.com/page5.html

單獨使用 Apache 可以嗎?如果沒有,我已經編寫了一個 PHP 腳本來根據傳入的主機名稱在 iframe 中加載 site.example.com/page*.html 頁面,但我不確定如何告訴 Apache 發送這 5 個 lp*。域到該PHP 腳本。

我希望我能很好地解釋這個項目。我們基本上需要將每個 lp* 子網域重新導向到不同的 site.example.com/page*.html 頁面,而不更改網址列中的位址。

提示和建議將不勝感激。我所能做的就是讓 Apache 將這些 URL 重定向到我的 PHP 腳本,但是網址列發生了變化,因為它是重定向,所以網址列也發生了變化(此時 PHP 腳本不起作用,因為主機名稱已更改)

答案1

使用相同的主機名為每個主機名稱設定一個虛擬主機DocumentRoot,但使用:

DirectoryIndex page1.html

對於 lp1,

DirectoryIndex page2.html

對於 lp2 等,以便這些頁面用作預設頁面而不是index.html.

因此,eghttp://lp3.othersite.com/將提供服務page3.html。它還允許您導航到http://lp3.othersite.com/page1.html等,但預設頁面將是您透過DirectoryIndex指令指定的頁面。

答案2

也許我錯過了一些東西,但我認為您所要求的只是虛擬主機。https://httpd.apache.org/docs/2.2/vhosts/

虛擬主機需要將多個 DNS 項目設定為相同 Web 伺服器。 Apache 根據所使用的 DNS 名稱提供指定頁面。除非您重定向,否則用戶會看到他們鍵入的內容。

答案3

您可以使用 URL 重寫來做到這一點。只是一個快速的即興示例(可能有效也可能無效):

RewriteCond %{HTTP_HOST} ^lp([0-9]).othersite.com [NC] 
RewriteRule ^(.*)$ page$1.html [NC,QSA] 

答案4

跟隨Apache 虛擬主機文檔VirtualHost為每個 FQDN設置,然後使用模組目錄設定DirectoryIndex並用於重定向使用模組重寫

教學:

如何使用 Apache 和 Nginx 建立臨時和永久重新導向

相關內容