带 www 前缀的 ASP.NET HTTP 到 HTTPS 重定向
我正在使用这个简单的代码将 http 重定向到我的计费登录页面上的 https:
if (!Request.IsSecureConnection)
{
// send user to SSL
string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);
}
我还需要它检查 www 并将其添加到 url(如果它不在 url 中)。我需要在这段代码中添加什么来完成这个任务?
I am using this simple code to redirect http to https on my billing landing page:
if (!Request.IsSecureConnection)
{
// send user to SSL
string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);
}
I need it to also check for and add www to the url if it is not already in the url. What do I need to add to this code to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码假设服务器名称不以“www”开头。那么补救措施就是在当前服务器名前面加上“www”。
就我个人而言,我不喜欢这种做事方法。我通常创建一个名为
SecureDomain
之类的设置,然后使用逻辑来验证当前的 ServerName 是否与之匹配。像这样的东西。The following code assumes that if the server name doesn't start with a "www." then the remedy is to prepend whatever the current servername is with "www."
Personally, I don't like this method of doing things. I usually create a setting named something like
SecureDomain
and then use logic to verify whether the current ServerName matches that. Something like this.像这样:
Like this: