How to redirect to another page

P

Peter Afonin

Hello:

I need to redirect user to another folder (or another site). Usually I put
this code to global.asax:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim svr As String = LCase(CStr(Request.Url.ToString))
Dim svr2 As String
If InStr(1, svr, "domain1") > 0 Then
svr2 = svr.Replace("domain1", "domain2")
Response.Redirect(svr2)
Else
Response.Redirect(svr)
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
End Try

(I can also use
Dim svr As String = LCase(CStr(Request.ServerVariables("server_name"))))

It works great as long as I redirect to another ASPX page. However, this
doesn't work (event in global.asax doesn't fire) if I want to redirect to
HTML page or another file. For instance, if user types:

http://domain1.com/page.htm

I want him to go to http://domain2.com/page.htm

In my particular case I want him to be able to download a file, so if I give
him a link like this:

http://domain1.com/file.zip

I want him to download http://domain2.com/file.zip.

Same with just another folder instead of domain.

How would I do this?

I would appreciate your help.

Thank you,
 
S

Scott M.

I've never had a problem using Response.Redirect(), but you could write some
client side JavaScript that says:

document.url = newURL

to the client.
 
S

Sherif ElMetainy

Hello

By default IIS is not configured to let ASP.NET handle html and other static
files. So your code never gets executed when it is a .htm file is requested.
To to this, configure IIS to make aspnet_isapi.dll handle requests for files
with .htm extension.

Best regards,
Sherif
 
S

Scott M.

That's not what the OP was saying. He didn't say his server code in a
client page wasn't being executed. He wanted to know how a server page can
redirect to a non server page. IIS does not have to be reconfigured to do
this.


Sherif ElMetainy said:
Hello

By default IIS is not configured to let ASP.NET handle html and other static
files. So your code never gets executed when it is a .htm file is requested.
To to this, configure IIS to make aspnet_isapi.dll handle requests for files
with .htm extension.

Best regards,
Sherif
 
P

Peter Afonin

Thank you everyone for your help.

Sherif was correct. After I added this association in IIS, the redirection
worked.

Perhaps I wasn't clear enough in my explanations.

This is exactly what I'm trying to do in this example:

I have two applications in my localhost: redirect and manager. If I type
http://localhost/redirect/faq.htm, I want it to be redirected to
http://localhost/manager/faq.htm (in the real world it will be probably a
different domain).

Now, after I created the associations in IIS, as Sherif pointed out, it
works. If I type http://localhost/redirect/faq.htm it redirects it to
http://localhost/manager/faq.htm, I can see it in the browser's address bar.

There is one weird thing is going on, however: faq.htm must be present in
BOTH applications. If faq.htm is present in manager application, but doesn't
exist in the redirect application, I'm getting "page not found" error.

This is something I cannot explain. The code is executed in global.asax,
when I debug, everything looks correct. Why faq.htm must be present in
redirect then?

Any ideas?

Thank you,

Peter

Scott M. said:
That's not what the OP was saying. He didn't say his server code in a
client page wasn't being executed. He wanted to know how a server page can
redirect to a non server page. IIS does not have to be reconfigured to do
this.
 
S

Scott M.

There is no need to add file associations to do redirection in an .htm page.
Simply add an event handler that uses:

document.url = new url

What you are trying to do is very simple. I think you are over-engineering
it.
 
S

Sherif ElMetainy

Hello

In the dialog box where you set the associations in IIS, there is a check
box that says "Verify that file exists", make sure that this check box is
not checked.

Best regards,
Sherif
 
P

Peter Afonin

Thank you very much.

Scott, perhaps this can be done much easier using JavaScript, as you
suggested. Unfortunately, I'm not an expert in JS, so I'm trying to do it in
ASP.Net.

Sherif, you were correct again. After I unchecked this box, everything works
the way I need.

Thanks again,

Peter
 
P

Peter Afonin

Scott, if you could give me more details on how exactly you would do it in
JavaScript, using my example, I would greatly appreciate it. Yes, it would
be easier to do it this way than to ask my hosting provider to configure
IIS.

Thank you,

Peter
 
S

Scott M.

------ First Page ----------------------------

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function redirect()
{
document.location = "page2.htm"
}
</SCRIPT>
</HEAD>
<BODY>
<Input type="button" onClick="redirect()" Value="Go to page 2">
</BODY>
<HTML>

------ Second Page ----------------------------

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function getZip()
{
document.location = "file.zip"
}
</SCRIPT>
</HEAD>
<BODY>
<Input type="button" onClick="getZip()" Value="Get zip file">
</BODY>
<HTML>

------ NOTES ----------------------------

I'm using the onClick event handler of a button to cause the redirect here,
but that's just one event handler you could use. You could use a JavaScript
timer to automatically redirect after a certain number of seconds, you could
do it when a user passes the mouse over a certain page element, etc.

I'm using relative file paths here, but you could replace them with URL's
(http://someOtherServer.com/file) as well.
 
P

Peter Afonin

Thank you, Scott.

But I think there is a big difference between what you are showing here and
what I was trying to achieve. In your example you should know exactly that
you're redirecting to page2.htm, or to file.zip. In my example I don't know
where exactly the user wants to go. It may be page2.htm, or page3.htm, or
file.zip, or anything. The main idea was to replace just one portion of the
URL: if user goes to www.domain.com/site1/page2.htm, he would be redirected
to www.domain.com/site2/page2.htm. But he may try to go to page3.htm,
page10.htm, file.zip - it doesn't matter. The only portion of the URL is
replaced is site1 to site2. That's why I used the Replace method:

svr2 = svr.Replace("domain1", "domain2")

Please correct me if I misunderstood you.

Peter
 
S

Scott M.

This is not a problem Peter. The code I showed was static. But it can be
generated at the server level and sent down to the client. So the actual
URL doesn't have to be hard coded at all.

My main point was to show you how to redirect at the client.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top