Brian
asked on
URL Rewrite
Hi EE,
I need to redirect users in my web.config file if a user tries to enter http://mysite.com/secure which should be https://mysite.com/secure. Is there a way to do this in the web.config file such as URL Rewrite? I don't have access to IIS since I'm on a Shared Hosting Plan so it needs to be done in web.config file.
Thanks in advance!!!
I need to redirect users in my web.config file if a user tries to enter http://mysite.com/secure which should be https://mysite.com/secure. Is there a way to do this in the web.config file such as URL Rewrite? I don't have access to IIS since I'm on a Shared Hosting Plan so it needs to be done in web.config file.
Thanks in advance!!!
ASKER
Hi Yiogi,
Ok, but where do I put the URL that I want to have redirected to https at?
Ok, but where do I put the URL that I want to have redirected to https at?
ASKER
That does not show how to redirect a user who visits http://www.mysite.com to https://www.mysite.com. I need to have the http site redirected to https. Can you show me how to do this?
add global.asax file from right click on project in solution explorer->add new items->add global.asax file.
Put this code:
protected void Application_BeginRequest(O bject sender, EventArgs e)
{
if (!HttpContext.Current.Requ est.IsSecu reConnecti on)
{
Response.Redirect("https://" + Request.ServerVariables["H TTP_HOST"]
+ HttpContext.Current.Reques t.RawUrl);
}
}
Put this code:
protected void Application_BeginRequest(O
{
if (!HttpContext.Current.Requ
{
Response.Redirect("https://" + Request.ServerVariables["H
+ HttpContext.Current.Reques
}
}
You will not put the url to redirect to anywhere. It will redirect the user to the exact same url he typed in the address but to secure version of the page instead. So if for example user navigated to:
http://www.mysite.com/test/default.aspx?asds=value
he will be redirected to:
https://www.mysite.com/test/default.aspx?asds=value
This is exactly what Request.ServerVariables["H TTP_HOST"] + HttpContext.Current.Reques t.RawUrl
will do for you. Replace the url with the one the user originally navigated to. That is the beauty of it.
http://www.mysite.com/test/default.aspx?asds=value
he will be redirected to:
https://www.mysite.com/test/default.aspx?asds=value
This is exactly what Request.ServerVariables["H
will do for you. Replace the url with the one the user originally navigated to. That is the beauty of it.
ASKER
Hi Yiogi,
Not sure where to put the http URL at in the following code that you provided below.
protected void Application_BeginRequest(O bject sender, EventArgs e)
{
if (!HttpContext.Current.Requ est.IsSecu reConnecti on)
{
Response.Redirect("https://" + Request.ServerVariables["H TTP_HOST"]
+ HttpContext.Current.Reques t.RawUrl);
}
}
Not sure where to put the http URL at in the following code that you provided below.
protected void Application_BeginRequest(O
{
if (!HttpContext.Current.Requ
{
Response.Redirect("https://" + Request.ServerVariables["H
+ HttpContext.Current.Reques
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Yiogi,
No, still confused, you are showing the same URL's above. You also mention asp.net as an example but not in the code you provided. All my pages DO NOT require SSL only a certain directory such as "client/secure/index.aspx" so i need to make sure that if someone types in http://mysite.com/client/secure/index.aspx they get redirected to https://mysite.com/client/secure/index.aspx.
No, still confused, you are showing the same URL's above. You also mention asp.net as an example but not in the code you provided. All my pages DO NOT require SSL only a certain directory such as "client/secure/index.aspx"
Ah ok, I apologize I thought you wanted it for all the pages.
The code I have there will redirect the user to SSL for any page in the website if you place it in the global.asax file.
Just place the code below in your index.aspx.cs then. And you are all set.
The code I have there will redirect the user to SSL for any page in the website if you place it in the global.asax file.
Just place the code below in your index.aspx.cs then. And you are all set.
protected override void OnInit(EventArgs e) {
base.OnInit(e);
if (!HttpContext.Current.Request.IsSecureConnection) {
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}
}
Open in new window