[英]IIS Rewrite rule - ignore for localhost
我有以下規則,它可以很好地將我的www請求重定向到根目錄。
但是我似乎無法將其關閉為localhost。 這就是我現在擁有的:
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
我嘗試了很多東西,包括:
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
你能幫忙嗎? 正則表達是我的弱點唉
如何使用條件僅匹配以www.
開頭的請求www.
當你不希望規則適用時,而不是試圖否定? 這避免了否定localhost
的需要,因為localhost
在條件中永遠不會匹配:
<rule name="Strip WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{URL}" />
</rule>
但是,您嘗試的規則示例(第二個代碼塊)也適用於在Windows 10 VM上使用IIS進行測試。 我可以在沒有重定向的情況下瀏覽到localhost
。 也許這里還有另一個問題。
我不會混合不同托管環境的規則; localhost(用於本地開發)和www,您的實時環境。 如果將它們分開,則不必根據環境啟用和禁用規則。
rules
部分有一個configSource
屬性,您可以通過該屬性指向另一個單獨的文件,例如。 RewriteRules.config
。 這樣做, web.config
將如下所示。
<configuration>
<!-- Other settings go here. -->
<system.webServer>
<!-- other settings go here --->
<rewrite>
<rules configSource="RewriteRules.config">
</rewrite>
</system.webServer>
</configuration>
RewriteRules.config
文件包含規則。
<rules>
<rule name="CanonicalHostNameRule1">
<!-- Rule details go here -->
</rule>
</rules>
您為每個環境創建此RewriteRules.config
文件的separte版本,僅包含適當的規則並將其部署到相關的Web服務器。
這有很多好處。
RewriteRules.config
文件的部署可以包含在部署自動化中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.