使用IIS URL将非www重定向到www通常无需硬编码域或TLCD重写 [英] Redirect non-www to www with IIS URL Rewrite generically without hardcoding domain or TLCD

查看:196
本文介绍了使用IIS URL将非www重定向到www通常无需硬编码域或TLCD重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个ASP.NET CMS驱动的Web应用程序,它将为不同域名下的多个网站提供服务。其中一些将使用www子域,其他将使用自定义子域。将有各种顶级国家/地区域。

I am building an ASP.NET CMS-driven web application which will serve multiple websites under different domain names. Some of these will use www sub-domain, others will use custom sub-domains. There will be a variety of top-level country domains.

我正在寻找一个通用的IIS URL重写规则,该规则将重定向任何未指定子网址的URL -domain到它的www等价物。

I'm looking for a generic IIS URL Rewrite rule that will redirect any URL which doesn't specify a sub-domain to its www equivalent.

当我说泛型时,它意味着该规则不能对域名或顶级国家域进行硬编码。因此规则必须将
http://anything.anywhere/any-path 重定向到 http://www.anything.anywhere/any-path 但是留下 http://sub.anything.anywhere/any-path

When I say generic it means the rule cannot hard-code either domain name or top-level country domain. So the rule must redirect http://anything.anywhere/any-path to http://www.anything.anywhere/any-path but leave http://sub.anything.anywhere/any-path.

我发现的最近的是这个仍然硬编码TLCD。如果不了解URL Rewrite的语法,我不知道如何处理任何TLCD。

The closest I've found is this which still hard-codes TLCD. Without much knowledge of the syntax of URL Rewrite I'm not sure how to handle any TLCD.

提前致谢。

更新:

受到评论的启发,我已经开始使用正则表达式,但还没有找到一种方法,不需要我硬编码所有可能的TLCD列表。我怀疑这是我能得到的最好的。任何人都可以改进或确认这个答案吗?

Inspired by comment, I've had a go with regex, but haven't yet found a method that doesn't require me to hard-code a list of all possible TLCDs. I suspect this is the best I'll get. Can anyone refine or confirm this as the answer?

^([a-z]+[.](com|co.uk|de|fr|etc)+)*


推荐答案

我只是使用具有两个条件的重写规则完成相同的操作,一个用于获取Scheme,另一个用于确定是否缺少www。该方案是必要的,因为重定向必须是绝对的,但如果您不支持可以硬编码的HTTPS。请记住,我还没有时间测试HTTPS部分,但很确定它可以正常工作。

I just did the exact same thing using a rewrite rule with two conditions, one to get the Scheme and one to determine if the www is missing. The scheme is necessary as the redirect has to be absolute, but if your not catering for HTTPS that could be hard-coded. Just bear in mind I've not had time to test the HTTPS part yet, but pretty sure it will work ok.

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Root Redirect" stopProcessing="true">
                        <match url=".*" negate="false" />
                        <action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" />
                        <conditions trackAllCaptures="true">
                            <add input="{CACHE_URL}" pattern="^(.*)://" />
                            <add input="{HTTP_HOST}" pattern="^(?!www\.).*" />
                        </conditions>
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

这篇关于使用IIS URL将非www重定向到www通常无需硬编码域或TLCD重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆