如何創建一個 Htaccess,該 Htaccess 將為本地主機和生產具有特定且單獨的代碼。我的意思是當我們在 localhost 上作業時,它應該是作業 localhost 代碼,而在生產中它應該只加載生產代碼。這樣我就可以將一個 Htaccess 用于本地和生產,這將節省大量時間。以下是我想要實作的模型。任何人都可以對此提供幫助,我們將不勝感激。因為我花了很多時間在這上面并沒有找到任何好的方法。提前致謝!
<Localhost>
---localhost code goes here
---it should be only work in localhost and not at all in Production
</localhost>
<Production>
---production code goes here
---it should be only work in Production and not at all in Localhost
</Producton>
uj5u.com熱心網友回復:
大多數代碼在.htaccess本地和生產中應該是相同的。(否則,你如何測驗它?)
但是,在服務器之間分離指令的最干凈的方法之一是Define(需要 Apache 2.4)在其中一臺服務器(例如開發機器)的服務器配置中添加一個變數:
Define DEVELOPMENT
這可以在服務器配置中的任何地方定義(不是.htaccess),但始終被視為服務器的全域,無論它是否在<VirtualHost>容器內定義。您不需要指定值(第二個)引數,因為<IfDefine>指令(見下文)不檢查這一點。與往常一樣,每當您對服務器配置進行更改時,您都需要重新啟動 Apache 以使更改生效。
并在.htaccess:
<IfDefine DEVELOPMENT>
# Local / development directives only
</IfDefine>
<IfDefine !DEVELOPMENT>
# Live / production directives only
</IfDefine>
該!前綴測驗,該變數沒有定義。
根據您需要包含的指令型別,您可以使用<If>容器并檢查諸如請求的主機名之類的內容(例如staging.example.com,www.example.com針對實時站點)。但是,<If>容器與 mod_rewrite 的作業方式不同。
參考:
- https://httpd.apache.org/docs/current/mod/core.html#define
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322382.html
上一篇:將通用頁面重定向到特定
