将旧域名到新域名的重定向添加到现有的IIS web.config 重定向规则中。

huangapple go评论97阅读模式
英文:

Add old-to-new domain redirect into existing redirect rules in web.config for IIS

问题

以下是代码的翻译部分:

<rewrite>
    <rules>
        <rule name="将请求重定向到 https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" negate="false" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
        </rule>

        <rule name="重定向 - 顶级域名从非 www 到 www" stopProcessing="true">
            <match url="^_*(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+?)$" />
            </conditions>
            <action type="Redirect" url="{MapSSL:{HTTPS}}www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>

        <rule name="添加斜杠" stopProcessing="true">
            <match url="(.*[^/])$" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{URL}" pattern="/umbraco_client" negate="true" />
                <add input="{URL}" pattern="/setsquare" negate="true" />
                <add input="{URL}" pattern="/install" negate="true" />
                <add input="{URL}" pattern=".axd" negate="true" />
                <add input="{URL}" pattern=".webmanifest" negate="true" />
                <add input="{URL}" pattern="/signin-google" negate="true" />
                <add input="{URL}" pattern="/assets" negate="true" />
            </conditions>
            <action type="Redirect" url="{R:1}/" />
        </rule>
    </rules>
    <rewriteMaps>
        <rewriteMap name="MapSSL" defaultValue="http://">
            <add key="ON" value="https://" />
            <add key="OFF" value="http://" />
        </rewriteMap>
    </rewriteMaps>
</rewrite>

<rule name="从旧域名到新域名的重定向" enabled="true">
    <match url="^olddomain\.com$" />
    <action type="Redirect" url="https://www.newdomain.com" redirectType="Permanent" />
</rule>

请注意,这是您提供的代码的翻译部分,没有其他内容。如果您有任何问题或需要进一步的帮助,请随时告诉我。

英文:

I have a multi-domain website which does some common redirects for things like enforcing https and www and trailing slashes. The code in web.config is as follows:

&lt;rewrite&gt;
&lt;rules&gt;
&lt;rule name=&quot;Redirect to https&quot; enabled=&quot;true&quot; patternSyntax=&quot;Wildcard&quot; stopProcessing=&quot;true&quot;&gt;
&lt;match url=&quot;*&quot; negate=&quot;false&quot; /&gt;
&lt;conditions logicalGrouping=&quot;MatchAny&quot;&gt;
&lt;add input=&quot;{HTTPS}&quot; pattern=&quot;off&quot; /&gt;
&lt;/conditions&gt;
&lt;action type=&quot;Redirect&quot; url=&quot;https://{HTTP_HOST}{REQUEST_URI}&quot; redirectType=&quot;Permanent&quot; /&gt;
&lt;/rule&gt;
&lt;rule name=&quot;Redirect - Top domains with non-www to www&quot; stopProcessing=&quot;true&quot;&gt;
&lt;match url=&quot;^_*(.*)&quot; /&gt;
&lt;conditions logicalGrouping=&quot;MatchAll&quot; trackAllCaptures=&quot;false&quot;&gt;
&lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^([^\.]+)\.([^\.]+?)$&quot; /&gt;
&lt;/conditions&gt;
&lt;action type=&quot;Redirect&quot; url=&quot;{MapSSL:{HTTPS}}www.{HTTP_HOST}/{R:1}&quot; redirectType=&quot;Permanent&quot; /&gt;
&lt;/rule&gt;
&lt;rule name=&quot;Add trailing slash&quot; stopProcessing=&quot;true&quot;&gt;
&lt;match url=&quot;(.*[^/])$&quot; /&gt;
&lt;conditions&gt;
&lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsDirectory&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsFile&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;/umbraco_client&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;/setsquare&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;/install&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;.axd&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;.webmanifest&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;/signin-google&quot; negate=&quot;true&quot; /&gt;
&lt;add input=&quot;{URL}&quot; pattern=&quot;/assets&quot; negate=&quot;true&quot; /&gt;
&lt;/conditions&gt;
&lt;action type=&quot;Redirect&quot; url=&quot;{R:1}/&quot; /&gt;
&lt;/rule&gt;
&lt;/rules&gt;
&lt;rewriteMaps&gt;
&lt;rewriteMap name=&quot;MapSSL&quot; defaultValue=&quot;http://&quot;&gt;
&lt;add key=&quot;ON&quot; value=&quot;https://&quot; /&gt;
&lt;add key=&quot;OFF&quot; value=&quot;http://&quot; /&gt;
&lt;/rewriteMap&gt;
&lt;/rewriteMaps&gt;
&lt;/rewrite&gt;

This works fine. However, there's now a requirement to redirect one of the inbound domains (olddomain.com) to a new domain (newdomain.com), so I've added the following rule after the first ('Redirect to https') rule:

&lt;rule name=&quot;Old to new domain&quot; enabled=&quot;true&quot;&gt;
&lt;match url=&quot;^olddomain\.com$&quot; /&gt;
&lt;action type=&quot;Redirect&quot; url=&quot;https://www.newdomain.com&quot; redirectType=&quot;Permanent&quot; /&gt;
&lt;/rule&gt;

It doesn't need to maintain any of the old URLs, just any request into olddomain.com (e.g. olddomain.com/whatever) can redirect to the root 'newdomain.com' domain.

Can anyone advise how to correct the new rule since it doesn't seem to do anything?

Many thanks.

答案1

得分: 0

正则表达式你写的可能是正确的(不确定)。但当查看我写的其中一个规则来将非www重定向到www时,我不需要转义域名中的句点。也许这样可以:

<rule name="OldToNewDomain" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^olddomain.com$" />
  </conditions>
  <action type="Redirect" url="http://www.example.com/" redirectType="Permanent" />
</rule>

来源: Web.config redirects with rewrite rules - https, www, and more

英文:

The regex you've written may be right (not sure). But when looking at one of the rules I've written to redirect non-www to www, I don't need to escape the period in the domain name. Maybe this works:

&lt;rule name=&quot;OldToNewDomain&quot; stopProcessing=&quot;true&quot;&gt;
  &lt;match url=&quot;(.*)&quot; /&gt;
  &lt;conditions&gt;
    &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^olddomain.com$&quot; /&gt;
  &lt;/conditions&gt;
  &lt;action type=&quot;Redirect&quot; url=&quot;http://www.example.com/&quot; redirectType=&quot;Permanent&quot; /&gt;
&lt;/rule&gt;

Source: Web.config redirects with rewrite rules - https, www, and more

huangapple
  • 本文由 发表于 2023年3月8日 17:33:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定