14

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

WhiteKnight
  • 4,938
  • 5
  • 37
  • 41
CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109

6 Answers6

16

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>
CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109
  • 1
    I don't know if 'upgrading to IIS7' is really an answer to this problem. – ChadT Jan 27 '11 at 08:21
  • This won't work with postbacks... (you'll be redirected every time you take some action that POSTs page - like button click) – nikib3ro May 18 '11 at 19:31
  • perhaps, but i would think that would depend on the action on the form, whether it's set to default.aspx or set to / (can't say for sure though, haven't had a need to try that level) – CodeMonkey1313 May 20 '11 at 00:49
  • @kape123 - You can add a condition if you want which only applies this redirect to GET requests. The rewrite module should allow you to build that condition easily... http://www.iis.net/download/urlrewrite – Steve Wortham Mar 22 '12 at 14:31
  • 1
    The second rule (for handling Default.aspx in all folders) only worked for me after a small change to: type="Redirect" redirectType="Permanent" rather than simply type="Rewrite". – PolyTekPatrick Oct 06 '13 at 11:57
5

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.

Bruce Chapman
  • 1,227
  • 3
  • 12
  • 19
2

I had the same problem. For those who wonder why anyone would want to do this, it's a question of SEO. If Google indexes your home page with and without the default.aspx at the end, the PageRank and link popularity will be split between the two URL's. Now, if you're experiencing this problem, and you're able to consolidate the two URL's then you may get a boost in search rankings. One more thing to keep in mind is that if you're going through the trouble, you MUST use a 301 redirect for Google to consolidate their index between two URL's. Otherwise your efforts will be futile.

This is a little too late since you've already solved this by upgrading to IIS7. But I'll just add that the only solution to this problem I've come up with for IIS6 is to add an ISAPI filter.

I documented the complete solution here... http://swortham.blogspot.com/2008/12/redirecting-default-page-defaultaspx-to.html

Steve Wortham
  • 21,740
  • 5
  • 68
  • 90
1

If I understand you correctly, you don't want to display 'default.aspx' whenever someone comes into a folder with that document available.

So if they do hit it, you want to automatically redirect to the '/' and just load the default document anyway?

If that's the case then, as stated above, you run the risk of an infinite loop. The second comment gives you an answer but I guess expanding that to the re-write engine what you'd want is to:

Turn off default documents Register each folder with the re-write engine When that folder is requested load the default.aspx file as per your target rule

Does this sound about right?

I have to ask, why do you want to do this?

Simon
  • 984
  • 1
  • 8
  • 24
  • I know it's pretty strange, but it's a requirement of the site, for SEO value, so that only one link will take you to default.aspx, rather than two. – CodeMonkey1313 Mar 27 '09 at 12:10
  • I've done a fair bit of SEO training and hearing this seems odd. I'd be surprised that a search engine like Google or MSN would penalise you. So I can only guess its so you get all your ranking points for one url. Still - haven't done SEO stuff in a little while so that's just a guess! – Simon Mar 27 '09 at 13:14
  • 1
    As I understand it, it isn't that there are two pages that serve up the same page, it's that if people link to both example.com and example.com/default.aspx I'm told that it spreads out link value. – CodeMonkey1313 Apr 01 '09 at 12:17
  • You are right, 2 pages with the same content is not as good as 1 page: http://www.google.com/support/webmasters/bin/answer.py?answer=66359 – BigBlondeViking Aug 21 '09 at 13:39
0

I'm not sure I understand what the problem is.

Though if you turn off default documents then / will simply point to the directory rather than the default.aspx page.

Leave default documents on and just do a redirect based on whether default.aspx is in the requested url or not.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
0

well you can use regular .net to inspect httprequest url, if it has "default.aspx" in it, you can redirect to "/", there will be no infinite loop and you better do this on preload, and end response afterwards, to minimize time it takes to process

Ayyash
  • 4,257
  • 9
  • 39
  • 58