15

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

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

http://www.example.com/Default.aspx

to:

http://www.example.com/

My question is:

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

HttpContext.Current.Request.Url.*

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

gunwin
  • 4,578
  • 5
  • 37
  • 59
  • 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 05 '12 at 07: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 '14 at 15:42

10 Answers10

11

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

string path = Request.RawUrl;

MSDN

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • 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' – gunwin Dec 31 '11 at 17:41
  • 3
    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 06 '12 at 19:34
  • 1
    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 '14 at 15:33
4

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?

undefined
  • 33,537
  • 22
  • 129
  • 198
  • 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. – undefined Jan 01 '12 at 05:23
4

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: https://stackoverflow.com/questions/363231/...

Community
  • 1
  • 1
Gabriel GM
  • 6,391
  • 2
  • 31
  • 34
4

For a request to:

http://domain.com/

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.

RickNZ
  • 18,448
  • 3
  • 51
  • 66
2

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:

<link rel="canonical" href="http://www.mysite.com/" />

Google will always index

http://www.mysite.com/Default.aspx

as

http://www.mysite.com/

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

Jamie Carruthers
  • 685
  • 1
  • 8
  • 22
1

You may try

HttpContext.Current.Request.Url.*

with wrong parameters:

so get your site visitor url by :

Request.AppRelativeCurrentExecutionFilePath 

for more info:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.apprelativecurrentexecutionfilepath.aspx

Please provide more info and your try about your issue

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
  • -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
  • 2
    I undid my downvote. Edit: My vote is locked until you edit your post. – usr Dec 31 '11 at 20:49
1

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.

Community
  • 1
  • 1
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 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. –  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 /. –  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
0

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

0

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

Ken Nickel
  • 209
  • 1
  • 2
  • 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 04 '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 05 '12 at 04: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 06 '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 07 '12 at 07:00
0

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:

<rewrite>
  <rules>    
    <rule name="Default Document" stopProcessing="true">
      <match url="(.*?)/?default\.aspx$" ignoreCase="true" />
      <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
    </rule>   
  </rules>
</rewrite>

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>
justinlabenne
  • 793
  • 3
  • 6