英文:
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:
<rewrite>
<rules>
<rule name="Redirect to 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="Redirect - Top domains with non-www to 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="Add trailing slash" 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>
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:
<rule name="Old to new domain" enabled="true">
<match url="^olddomain\.com$" />
<action type="Redirect" url="https://www.newdomain.com" redirectType="Permanent" />
</rule>
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:
<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>
Source: Web.config redirects with rewrite rules - https, www, and more
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论