Reputation: 16011
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
Upvotes: 14
Views: 20524
Reputation: 1237
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.
Upvotes: 5
Reputation: 16011
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>
Upvotes: 16
Reputation: 22230
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
Upvotes: 2
Reputation: 4399
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
Upvotes: 0
Reputation: 994
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?
Upvotes: 1
Reputation: 35117
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.
Upvotes: 0