Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Rewrite from /default.aspx to /

I'm using the URL Rewriting.NET tool with IIS 6. I've got my default page content set for default.aspx in IIS. What I'm trying to do is have /default.aspx provide a 301 redirect to the root directory (www.example.com/default.aspx -> www.example.com). I've tried turning off default documents, to no avail.

What I'm hoping to do is use a couple of URL Rewriting.NET rules to accomplish this goal. Any thoughts?

EDIT:

Sorry, I forgot to clarify. If I redirect from /default.aspx to / with default documents turned on (I'd like to leave them on) then I get an infinite loop of default -> / -> default

like image 766
CodeMonkey1313 Avatar asked Mar 26 '09 16:03

CodeMonkey1313


People also ask

How does IIS URL Rewrite work?

The concept of URL rewriting is simple. When a client sends a request to the Web server for a particular URL, the URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.

What is a URL Rewrite rule?

A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful. Rewrite rules consists of the following parts: Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings.

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.


2 Answers

In the end I wound up using IIS 7 with the URL Rewrite module, which allows you to do this redirect properly.

Edit :

The rule is

<rule name="Default Redirect" stopProcessing="true">
    <match url="^default\.aspx$" />
    <action type="Redirect" url="/" redirectType="Permanent" />
</rule>

you can do that with a separate rule for each folder, or you can use

<rule name="All Redirect">
    <match url="^(.*\/)*default\.aspx$" />
    <action type="Rewrite" url="{R:1}" />
</rule>
like image 113
CodeMonkey1313 Avatar answered Nov 15 '22 20:11

CodeMonkey1313


I came across this very problem a while back while trying to work out why some IIS installs would work redirecting the /default.aspx and some would degenerate into a terminal loop.

I found the answer was whether or not asp.net was 'wildcard' mapped to run all requests within IIS.

Put simply, if you have an out-of-the-box IIS setup, it will always append the default document onto any request for the site root. Thus example.com becomes example.com/default.aspx when you inspect the Request.Url in ASP.NET. Therefore if you detect this situation and try to redirect away and back to example.com, IIS does so, appends the /default.aspx and your code is caught in a loop of it's own making.

The exception to this is if you set up wildcard mapping so that all requests are processed through the asp.net pipeline. In this case, IIS no longer appends the default document onto each request at the Request.Url level. And thus you can do the redirect.

I put it all in this blog post : 301 Redirecting from /default.aspx to the site root - the final word - but this was written several years back and changes in IIS7 may have fixed the problem, as the currently accepted answer provides.

But if you're battling this problem, then looking at the wildcard mapping status is the right place to start.

like image 28
Bruce Chapman Avatar answered Nov 15 '22 20:11

Bruce Chapman