在 url asp.net 网络表单 global.asax 中包含 www
include www in url asp.net web forms global.asax
我需要将用户重定向到与 url 中包含的 www 的安全连接我的代码适用于 https 连接,但如果用户键入 https mysite.com。它应该总是将用户重定向到 https www.mysite.com 下面是我的代码
if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
{
Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
Response.Status = "301 Moved Permanently";
Response.RedirectPermanent("https www.mysite.com" + HttpContext.Current.Request.RawUrl);
}
if (Request.Url.Host.ToString().StartsWith("www", StringComparison.OrdinalIgnoreCase).Equals(false))
{
Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
Response.Status = "301 Moved Permanently";
Response.RedirectPermanent("https www.mysite.com" + HttpContext.Current.Request.RawUrl);
}
谢谢指教VDWWD
<system.webServer>
<urlCompression doDynamicCompression="true" />
<rewrite>
<rules>
<clear />
<rule name="RedirectNonWwwToWww" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="LowercaseAllUrls" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
我需要将用户重定向到与 url 中包含的 www 的安全连接我的代码适用于 https 连接,但如果用户键入 https mysite.com。它应该总是将用户重定向到 https www.mysite.com 下面是我的代码
if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
{
Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
Response.Status = "301 Moved Permanently";
Response.RedirectPermanent("https www.mysite.com" + HttpContext.Current.Request.RawUrl);
}
if (Request.Url.Host.ToString().StartsWith("www", StringComparison.OrdinalIgnoreCase).Equals(false))
{
Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
Response.Status = "301 Moved Permanently";
Response.RedirectPermanent("https www.mysite.com" + HttpContext.Current.Request.RawUrl);
}
谢谢指教VDWWD
<system.webServer>
<urlCompression doDynamicCompression="true" />
<rewrite>
<rules>
<clear />
<rule name="RedirectNonWwwToWww" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="LowercaseAllUrls" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>