IIS7 301重定向正则表达式用连字符替换下划线

时间:2014-10-01 16:03:01

标签: regex redirect iis-7 http-status-code-301

我正在寻找一些帮助,使用正则表达式来解决IIS7上的301重定向问题。

我有指向http://mydomain.co.uk/dir/colour_photo_copying.html等网页的链接,我希望将这些文件重命名为使用连字符而不是下划线。

在逐个的基础上,我可以使用以下方法执行此操作:

<location path="colour-photo-copying.html">
    <system.webServer>
        <httpRedirect enabled="true" destination="colour_photo_copying.html" httpResponseStatus="Permanent" />
    </system.webServer>
</location>

唯一的问题是我很少有这些并希望写一个正则表达式规则,但我不知道如何做到这一点。

我遇到过这个页面:IIS7 URL Rerwrite - how to replace all underscores with hyphens in a regex?但并不完全是我所希望的。

有任何正则表达的人帮忙吗?

非常感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

好的,我找到了这个解决方案并且认为我会分享以防其他任何人看到它。

<rewrite>
        <rules>
            <rule name="replace underscore with hyphen" stopProcessing="true">
                <match url="^(.*)_(.*).*$" ignoreCase="false" />
                <action url="/{R:1}-{R:2}" type="Redirect" />
            </rule>
        </rules>
    </rewrite>

如果有人觉得他们可以改进,请告诉我。

谢谢, 雅

相关问题