Take the 2-minute tour ×

I am writing in the Application_BeginRequest part of my Glabal.asax.cs.

For SEO purposes, I'm trying to redirect users that are looking at:




My question is:

How can I tell which the user is looking at? I've been using:


But all the parameters are identical regardless of which one I am visiting.

share|improve this question
Are you using ASP.NET Routing? If so, what does your routing look like? –  Russ Cam Dec 31 '11 at 18:36
I'm curious how this helps SEO, wouldn't a 403 penalize you? Regardless, I believe an httpmodule can do this.. is that an option? –  itchi Jan 5 '12 at 7:41
I'm trying to to the same thing for the same reason - I've been told that having several URLs for the same page is considered a bad thing so what I want to do is detect a request for "/default.aspx" and issue a 301 redirect to "/". RickNZ's answer seems a bit complex - I'm interested to see if you got a simpler solution. –  Andy Jan 22 at 15:42
add comment

10 Answers

up vote 4 down vote accepted

For a request to:


when referenced from the Begin_Request event in an HttpModule:

HttpContext.HttpRequest.Url.AbsolutePath == "/"
... Url.AbsoluteUri = "http://domain.com/"
... Url.LocalPath = "/"

If you're not seeing those values from Application_BeginRequest, you might need to register a separate HttpModule. Global.asax events are only called for managed HttpHandlers, so they may not be called until after IIS does an internal redirect (transfer) to "/default.aspx".

You may also need to disable default document handling in IIS for this to work in the direction you're interested in (most sites redirect from / to /default.aspx, not the other way around).

Edit: another idea. Register a new extension as an ASP.NET page handler, with a build provider (in web.config) -- maybe *.asph or something like that. Then rename your existing default.aspx to default.asph. Next, add default.asph to the top of the list of default files in IIS, replacing default.aspx. Create a new default.aspx file, whose only function in the code behind is to redirect to /. That should do it.

share|improve this answer
add comment

You can get the path entered in the user's browser with:

string path = Request.RawUrl;


share|improve this answer
If I was navigating to 'http:// www.site.com/' and the default page was 'Default.aspx'. This would return 'http:// www.site.com/Default.aspx' –  Grant unwin Dec 31 '11 at 17:41
Have you tried? It does what you want for me... –  Nicholas Butler Dec 31 '11 at 19:42
In my humble opinion you should not trust this method - Request.RawUrl returns anything beyond the root path (check MSDN) including querystrings... and it happens after IIS has done any redirecting at the beginning of the request. I would highly suggest you look at answers like RickNZ or Joshua's... you may also want to try the UrlRewrite module as well. –  one.beat.consumer Jan 6 '12 at 19:34
Nichoas and grant are both right - Nicholas for .Net 4.0 and after; grant for .Net 3.5 and earlier (at least according to a quick test I've done) –  Andy Jan 22 at 15:33
add comment

Im pretty sure this particular redirect happens at an IIS level not inside your application logic, prehaps turn off the default page stuff in IIS?

share|improve this answer
only works if the default page is static –  Joshua Dec 31 '11 at 18:37
This works also if it is dynamic. If you turn of the defaultdocumentmodule you just need to do the URL rewriting yourself. –  usr Dec 31 '11 at 20:35
Prehaps another way to deal with this it to not redirect from IIS to your default page, but instead redirect to a dummy page which Server.Transfers to Default.aspx. this would allow you to imply the actual url from which page it hits for your SEO stuff. –  Luke McGregor Jan 1 '12 at 5:23
add comment

If your are using IIS 7.x, you should look at this tutorial: http://weblogs.asp.net/scottgu/....
It explains how to setup redirect for SEO.

If you can't use the <rewrite> tag in your web.config, this post should help: http://stackoverflow.com/questions/363231/...

share|improve this answer
add comment

If it's just SEO you are concerned about, then you can use the the canonical "tag".

If you place this in your Default.aspx page:


Google will always index




preventing both pages being indexed and competing in the search results.

You can read more here: http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

share|improve this answer
add comment

If you request http://www.site.com/, it will redirect to http://www.site.com/default.aspx long before your code runs (this is visible to the browser of all things!).

I believe this means checking the value of HTTP-Referer will tell you what you want to know.

How to get HTTP Referer: Getting the HTTP Referrer in ASP.NET

Now then, if the user visited http://www.site.com, your code sees http://www.site.com/default.aspx called with referer of http://www.site.com. This doesn't tell you where they came from, but it does tell you they accessed the site directly.

You have the advantage here that most search engines follow the redirect and grab the actual page, so the referer value is usable to tell if the user came from a search engine or not. However, for other cases, you're better off walking the IIS logs.

Notice that due to the infrastructure in ASP.NET redirecting / to /default.aspx, you cannot redirect /default.aspx to / without creating an infinite loop. If you want to serve / directly, you're going to need a shim in front of IIS.

share|improve this answer
Checking the referrer will not help for the first loaded page on your site, only for the second, at which point it probably isn't of interest anymore. –  hvd Dec 31 '11 at 18:30
I see what you're trying now, but that assumes / redirects to /Default.aspx That doesn't necessarily happen: a typical IIS configuration doesn't do a client-side redirect from / to /Default.aspx, but loads and executes /Default.aspx directly when / is asked. And while it's possible to set it up so that your answer would work, consider then a user who visits the site, gets redirected to /Default.aspx, adds a bookmark, closes the browser, reopens the browser, visits the bookmark. The bookmark will cause the user to go straight to /Default.aspx without first visiting /. –  hvd Dec 31 '11 at 19:10
OK then I'm probably describing whatever configuration I have experience with, which I didn't set, but it doesn't have to be that way. –  Joshua Dec 31 '11 at 23:53
add comment

You may try


with wrong parameters:

so get your site visitor url by :


for more info:


Please provide more info and your try about your issue

share|improve this answer
Doesn't work... –  Grant unwin Dec 31 '11 at 17:40
-1 because it doesn't work. This answer should be below the other answers. –  usr Dec 31 '11 at 20:36
I think it is important to give answers that are known to be working because it adds the most value. This is the value that SO adds over Google. –  usr Dec 31 '11 at 20:45
I undid my downvote. Edit: My vote is locked until you edit your post. –  usr Dec 31 '11 at 20:49
add comment

Request.url will give you the current url which is opened in the browser window. You can use this to code something you want.

You can also do this by using iis. Set Default.aspx as your default page and whenever someone opens your site the address will be www.yoursite.com\

If you are trying to hide your current url from the user then its not recommended but you can use url rewrite for iis

share|improve this answer
add comment

Use a combination of routing and the IIS rewrite module. You may need to tweak this to suit a bit for the trailing slash (if you want one or not). I also have all my page names in lowercase. Using this will all but ensure a consistent 301 Permanent Redirect for your default.aspx page and fall in line with SEO best practices.

in Global.asax:

<%@ Import Namespace="System.Web.Routing" %>
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
        RouteTable.Routes.MapPageRoute("default", string.Empty, "~/default.aspx");       

and then in your web.config:

      <match url="(.*?)/?default\.aspx$" ignoreCase="true" />

If your not familiar with routing, you would then access the home page in a link as such:

<asp:HyperLink ID="homeLink" runat="server" NavigateUrl="<%$RouteUrl:RouteName=default %>">HOME</asp:HyperLink>
share|improve this answer
add comment

The best way to know what it's in the user's browser location is via javascript.

If you want to redirect everyone from Default.aspx back to just www.mysite.com, you can do something like this:

$(document).ready(function() { 
 if(window.location.href == "http://www.mysite.com/Default.aspx")
  window.location.href = "http://www.mysite.com/"

Note: this relies on jquery for $(document), but you get the idea

share|improve this answer
This doesn't answer the question which is about server side redirects, and won't be a benefit for SEO purposes as requested in the question. JavaScript redirects will not benefit SEO and may frustrate users and mess with the browser history (You know when you press back, then the browser just jumps forward again? Very annoying for your users!) –  BenSwayne Jan 4 '12 at 15:48
It answers the topic of the question: "How to tell if a user is visiting /Default.aspx or just /" –  voutmaster Jan 5 '12 at 4:59
He's looking for an answer in C#. Additionally, @BenSwayne is right that it will not benefit SEO since it's a 302 Temporary Redirect rather than a 301 Permanent Redirect. –  Derreck Dean Jan 6 '12 at 15:57
This answers the topic, but does not cover the seo need in the write up of the question. Plus, disabling JavaScript shuts it down entirely. –  justinlabenne Jan 7 '12 at 7:00
add comment

Your Answer


