views:

980

answers:

9

We have redesigned the structure to a website which has several business units. Now I want to redirect (301) to the new page.

IE:
was www.example.com/abc
now www.example.com/default.aspx?article=abc

I have tried to use Global.asax to do this, and it works properly when I debug through it.

        if (Request.RawUrl.Contains("abc"))
        {
            Response.RedirectLocation = "/default.aspx?article=abc";
            Response.StatusCode = 301;
            Response.StatusDescription = "Moved";
            Response.End();
        }

So http://localhost:1234/example/abc redirects properly, but (where 1234 is the port for the debugging server)
http://localhost/example/abc does not redirect, it gives me a 404.

Any ideas?


Additional info: If I go to http://localhost/example/abc/default.aspx then it redirects properly.

A: 

Have you made sure the web.config files are the same for each website (assuming :1234 is different to :80)

Also, have you tried localhost:80?

GateKiller
+1  A: 

Are you currently testing the site in the Visual Studio web server? That usually runs the site on "localhost:nnnnn" where "nnnnn" is a port number (as above is 1234), it doesn't set it to run without one.

If you've got IIS installed on the machine concerned, publish your project to it and you should be able to verify that it works without the "nnnnn" as there doesn't look to be anything in your code that would cause it to not do so.

Rob
+4  A: 

Well, if the port indicates you are using the built-in web server (the one that comes with VS), this probably works because that always routes requests through the ASP.NET framework.

Requests ending with /abc will not automatically route through the ASP.NET framework because IIS may not "know" you want them to. You need to check your IIS settings to make sure such requests are routed to the aspnet_isapi.dll


EDIT: To accomplish this, you need to add a wildcard mapping:

  1. In IIS Manager, expand the local computer, expand the Web Sites folder, right-click the Web site or virtual directory that you want, and then click Properties.
  2. Click the appropriate tab: Home Directory, Virtual Directory, or Directory.
  3. In the Application settings area, click Configuration, and then click the Mappings tab.
  4. To install a wildcard application map, do the following:
    • On the Mappings tab, click Add or Insert.
    • Type the path to the DLL in the Executable text box or click Browse to navigate to it (for example, the ASP.NET 2.0 dll is at c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll on my machine)
    • For extension, use ".*" without quotes, of course
    • Select which verbs you want to look for (GET,HEAD,POST,DEBUG are the usual for ASP.NET, you decide)
    • Make sure "Script engine" or "Application engine" is selected
    • Uncheck "Check that file exists"
    • Click okay.

I may be off on this, but if I am, hopefully someone will correct me. :)

Jason Bunting
How do I set this properly? I am looking at the Add/Edit Application Extension Mapping dialog.
Nathan Koop
It worked great, thanks alot
Nathan Koop
A: 

Your http://localhost/example/abc is not invoking the Global.asax like you expect. Typically http://localhost is running on port 80 (:80). If you want to run your site on port 80 you will need to deploy your site in IIS to run here.

SaaS Developer
A: 

You need to set up a Handler mapping in IIS to forward all unknown extensions to asp.net. The first one works because cassini is handling all requests, the second one doesn't work because IIS is looking for that directory, and it doesn't exist, instead of the .net framework running the code you have.

Here's information on how to do Url Rewriting in asp.net.

If possible though, i would suggest using the new Application Request Routing or UrlRewrite.net

Darren Kopp
+1  A: 

You should use the IIS wild card redirection, you will need something like this;

 *; www.example.com/*; www.example.com/default.aspx?article=$0

There is a reasonable reference at Microsoft

If you're using Apache I think you'll need to modify the htaccess file.

Dave Anderson
This might be a better way to handle the redirects instead of mapping everything through aspnet_isapi if all you need to be doing is redirects.There are also plugins for IIS that work like mod_rewrite in Apache, I don't have the websites off the top of my head, but they are out there.
Redbeard 0x0A
A: 

IIS, by default, doesn't hand all requests over to ASP.NET for handling. Only some resource extensions, among them "aspx" will be passed over to asp.net for handling. What's happening when you request http://localhost/example/abc is that IIS tries to locate the directory to see if you have a default file (i.e. default.aspx, index.html) to load from that directory. Since it can't find the directory with the junk "abc" tag in it, it never finds the default.aspx file to load.

When you try to load http://localhost/example/abc/default.aspx, IIS sees the "aspx" extension and immediately hands it over to the ASP.NET runtime for handling. The reason that the http://localhost/example/abc request doesn't load is that it never gets handed to ASP.NET, so of course the global.asax never sees it.

The Cassini hosted site handles all requests, thus that call does get handled by ASP.NET and the global.asax file.

I agree with Darren Kopp, who suggested that you need to set up Handler mapping in IIS to forward unknown extensions to ASP.NET.

A: 

Hello All, We are unable to add .* at wildcard application map as it give error of "Wrong file Extension". O.S. is 2003 server.

Please suggest the solution ASAP.