SSL and relative URLs in site links and redirectio

2019-09-16 09:52发布

问题:

I have been working on a website in beta phase for some time now, and am finally about to launch it. There are several links, anchor tags, with relative URLs throughout the site that link to the admin and cart sections of the website, and now they have to be SSL secured.

Also, same question for relative URLs in Response.Redirect("~/../..");

When a user is browsing over http, is there anyway to redirect them to a page with https connection using a relative URL? It seems like poor practice to code absolute URLs for links and redirections for the purpose of SSL. If the domain name changes, I have to rewrite them all. Plus, if I want them to work on my localhost, I would have to change them every time I upload to server. There must be some solution.

回答1:

Switching Between HTTP and HTTPS Automatically is a very good code to use for the implementation of switching logic fast and easy - and not change your existing code.

Similar: Preparing my ASP.NET / MVC site to use SSL?



回答2:

These helper methods by Rick Strahl will help you

http://www.west-wind.com/weblog/posts/2007/Sep/18/ResolveUrl-without-Page

Pay special attention to ResolveServerUrl.



回答3:

Depending on what version of IIS you are running, you could always offload this functionality to the webserver. Check out the URL Rewrite module here.