我正在尝试重定向到https
我在Global.asax
中使用了以下代码JSON
但我的问题是我必须在链接前添加www, 即mywesite.se和重定向到https之后的https://mywebsite.se,但我希望它像https://www.mywebsite.se
答案 0 :(得分:2)
使用UriBuilder:
var url = Context.Request.Url;
var builder = new UriBuilder(url);
builder.Scheme = "https";
if (!url.Host.StartsWith("www"))
builder.Host = "www." + url.Host;
Response.Redirect(builder.Uri);
免责声明:我没有测试此代码。
答案 1 :(得分:2)
您可以在web.config
<rewrite>
<rules>
<clear />
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^mywebsite.se$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.mywebsite.se/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
答案 2 :(得分:0)
这里你去(写入web.config文件)
void Application_BeginRequest(object sender, EventArgs e)
{
string lowerCaseURL = HttpContext.Current.Request.Url.ToString().ToLower();
if (lowerCaseURL.IndexOf("http://dotnetfunda.com") >= 0) // >= because http starts from the 0 position : )
{
lowerCaseURL = lowerCaseURL.Replace("http://dotnetfunda.com", "https://www.dotnetfunda.com");
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.Response.AddHeader("location", lowerCaseURL);
HttpContext.Current.Response.End();
}
}
将 dotnetfunda.com 替换为 yourwebsitedomainname.se
由于