[英]IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?
我一直在撞牆,試圖使一些IIS重定向規則起作用。 我已經在此處搜索並閱讀了Stack Overflow和IIS.net上的內容,但這根本不起作用。
我正在本地(真實)IIS上嘗試此操作,安裝了重寫2.0模塊,並嘗試在其上進行修復安裝。 我在admin cmd行執行iisreset的次數比我想提的要多。
在我的主機文件中,我為URL my.test.com設置了127.0.0.1。
我要實現的目標是使用一個額外的querystring參數將子域URL重定向到主域URL,同時保留現有的path和querystring值(如果存在)。
我在網站的根文件夾中設置了以下3條規則:
<system.webServer>
<rewrite>
<rules>
<rule name="PathAndQueryString" enabled="true" stopProcessing="true">
<match url="(/\w*)(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&param=value" appendQueryString="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="Querstring" enabled="true" stopProcessing="true">
<match url="(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}&param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://www.test.com?param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
不幸的是,盡管我嘗試使用Edge,Chrome,Firefox和IE對所有模式進行測試並確認{R:x}捕獲正確無誤,但對於所有這些IIS行為,規則似乎都不存在。
測試:
以上測試均無效,無需重定向即可獲得服務。
我還嘗試過將條件模式用作“匹配URL”模式的一部分,但它也不起作用。 實際上,在閱讀了Stack Overflow的帖子后,我將其移到了這種情況,該帖子說,當規則位於站點的根目錄時,它不包含主機,但仍然無效。
任何幫助將非常感激。
更新1:對於路徑和查詢字符串規則,嘗試從該模式的開頭( / \\ w *)刪除加粗部分。 沒有任何效果。
更新2:嘗試啟用極為氣質的“失敗的請求跟蹤”功能,並且在其實際工作時,它說匹配的URL模式均不匹配,但第三條規則已開始生效並重新定向,但並未維護該規則。 path和其他querystring參數的原因很明顯。
更新3:在Edge和IE上,所有規則都不起作用。 在Chrome和FF上,僅重定向域似乎可以正常工作-但是,如果我禁用了僅域規則,然后重新啟動IIS,它的行為就像規則仍然存在-FFS增強了我的實力,這種廢話真的開始使我沸騰了-這不應那么該死的困難。
在給出答案之前,我先向Microsoft的Yuk Ding大喊,他在iis.net論壇上回復了此帖子的副本,並提供了大部分答案。
因此,如果您要重寫規則以從1個URL重定向到另一個URL,並保留路徑和查詢字符串(如果存在),同時添加帶有適當的&或?的硬編碼URL參數,那么我們就來了。 這就是您需要的規則。
在規則中,您將從my.test.com重定向到www.test.com,但是顯然您需要替換以下規則中的這兩個URL,並將硬編碼參數“ ExtraParam = SomeValue”更改為所需的值。
<system.webServer>
<rewrite>
<rules>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="my.test.com" />
<add input="{REQUEST_URI}" pattern="/.+" negate="true" />
</conditions>
<action type="Redirect" url="http://www.test.com?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="PathOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="\?.+" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="Querstring" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{QUERY_STRING}" pattern=".*" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}&ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
在我看來,URL Rewrite 2.0模塊有一個錯誤。 基本的症結在於,您不能在主匹配模式中匹配任何內容,而必須對所有條件進行匹配。 因此,您可以為匹配設置模式這一事實是完全沒有意義的,因為如果您嘗試匹配(。*)以外的任何內容,即匹配我最初所做的任何事情,它都不會做您想要的事情,並且會導致頭腦混亂。撲通磚牆。
更煩人的是,在IIS管理器中,用於此操作的UI毫無幫助。 您可以測試您的模式,而我做到了,它們可以工作,但實際上測試的只是regEx模式。
我們真正需要的是在規則級別進行測試。 換句話說,您輸入一個URL,它告訴您任何規則是否匹配以及哪些條件通過或失敗。 是的,如果您啟用了失敗的請求跟蹤規則,則可以獲取此信息,但是所有這些都將被啟用,並且雜亂無章地工作(只需嘗試清除舊日志並觀察它,然后停止記錄直到您將其關閉,再來一次)對於一些瑣碎的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.