Scott Hanselman

Using ISAPI_Rewrite to canonicalize ASP.NET URLs and remove default.aspx

February 22, 2007 Comment on this post [18] Posted in ASP.NET | HttpModule | Musings | Tools
Sponsored By

In the comments of my post on Google PageRanks, Jeff Atwood says:

[The existence of] Default.aspx is another reason to consider URL rewriting. A few of my rewrite rules relative to PR:
- I don't allow links to come in as codinghorror.com, I add the www. if it is not there.
- I remove index.html if it is present

This got me thinking, as it appears that are quite a few ways to get to my home page.

You get the idea...Heck, probably just by mentioning them I'm getting in trouble, right? The URI that dare not speak its name.

Away, if we start by assuming my home page is http://www.hanselman.com/blog/ and that includes the trailing slash. We know that if my browser requests http://www.hanselman.com/blog without the slash, it'll be told by the Web Server to try it again anyway, which is just wasteful.

Apache folks have mod_rewrite and love to remind ASP.NET/IIS folks about their awesomeness. Many sites rely on mod_rewrite for certain behaviors. It's really a fundamental part of the Apache experience. The IIS story becomes better in newer versions of IIS, but the easiest and most flexible way to handle these kinds of things is ISAPI_Rewrite.

Sure, one could create an HTTP Module for ASP.NET for some of this, but at some point you'll realize that you need to catch these requests WAY earlier. Now, ISAPI_Rewrite uses Regular Expressions, and now it's time for my oft-repeated favorite RegEx joke - get ready for it:

"So you've got a problem, and you want to use Regular Expressions to solve it. Now you've got two problems."

Thanks for indulging me. Yes, writing ISAPI_Rewrite stuff is freaking voodoo and I hate it. Once you've written them, they're done. Here's mine:

[ISAPI_Rewrite]
RewriteRule /blog/default\.aspx http\://www.hanselman.com/blog/ [I,RP]

RewriteCond Host: ^hanselman\.com
RewriteRule (.*) http\://www.hanselman.com$1 [I,RP]

RewriteCond Host: ^computerzen\.com
RewriteRule (.*) http\://www.hanselman.com$1 [I,RP]

RewriteCond Host: ^www.computerzen\.com
RewriteRule (.*) http\://www.hanselman.com/blog/ [I,RP]

This rules normalize (canonicalize), to the best of my ability, all the not-really-good URLs above. It'll put everyone to http://www.hanselman.com/blog/ and even take totally lame links like http://computerzen.com/blog/GooglePageRanksConsideredSubtle.aspx and make then "correct." The "I" means "case insensitive" and the "RP" means "Redirect Permanently" - an HTTP 301. If it was just "R" it'd be a 302. When you're testing with ISAPI_Rewrite, always start with "R" to do temporary redirects, because you don't get a second chance with a 301.

So now, even if someone asks for http://www.hanselman.com/blog, they'll be told where to go(here's an HTTP conversation):

  • GET /blog HTTP/1.1
    • Heh, uh, get me /blog, m'kay?
  • HTTP/1.1 301 Moved Permanently
    Location:
    http://www.hanselman.com/blog/
  • GET /blog/ HTTP/1.1
    • Gosh, sorrey (my browser is Canadian) get me /blog/ then.

And it was Good™.

This kind of control is useful in any public facing application or web site and one should take an hour or so and really think about their website's "public face." ISAPI_Rewrite can be a powerful component as part of a larger ASP.NET solution, especially one where Google Ranks do matter and hackable or "pretty" URLs are highly valued.

For us, in the banking industry, having nice URLs like http://www.foobank.com/banking/ or http://mobile.foobank.com makes everyone happy.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Google PageRanks considered subtle

February 21, 2007 Comment on this post [17] Posted in Musings
Sponsored By

I did not know my Google PageRank until Phil mentioned it to me a while back. Apparently it's like the Richter (not Jeffrey Richter) scale in that a Page Rank of 6 is 10 times "better" than a Page Rank of 5, if I understand correctly.

Someone approached me to do advertising on the site, and since the bandwidth bill is due, I quoted a price I though was reasonable. She said, "but you only have a Page Rank of 5." This, for a moment, I become aware of this number since this advertiser cared.

I looked in the Google Toolbar and saw this:

Ok, looks like my Page Rank is 5, seems reasonable. However, later I noticed that if I was at http://www.hanselman.com/blog/ (note the lack of default.aspx) the Page Rank was 6. Seems like even though the home page is the home page, if there's a default.aspx at the end, that's a less "powerful" page.

I can only assume that more folks link to http://www.hanselman.com/blog than to the page with default.aspx. Apparently 10 times more, which seems reasonable.

I mentioned this to Phil who said, "weird, let me try" and sent me this screenshot where his Toolbar says my page is a 7. If I understand it, that's 100 times more shiny than a 5. Or, just +2. Who knows.

if Google's PageRank system is this subtle, and URIs aren't well canonicalized in their system then what's the point, Dear Reader? I know not. Seems like voodoo to me.

UPDATE: This post on the WebMaster group in response to another user says:

The page rank you see is not the pagerank Google uses.

- The pagerank you see is exported 3-4x/year

- It is "guessed" at whenever the page did not have a pagerank back
then. So if you have a "toolbar pagerank" (the one you see) TBPR 3 for
your homepage, and add a new sub-page, it will guess your sub-page to
be (perhaps) PR2, even though it doesn't have a real value for it yet.

- It is page-based ("page" rank :-)), not domain / site based

- Your sites internal interlinking structures determine how pagerank is
distributed among the pages - in the simplistic example where you have
a single page with is fed with pagerank (from the outside), you could
determine how that pagerank is spread among your pages based on the
link-structure in your site. You'll likely just give up if you have
more than 5 pages though :-) - it's not worth it.

- Your example with the homepage with a high PR and the other pages
having lower PR is perfectly normal and could be a "steady state"

Interesting stuff.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Tips on 2007 Conference Attendance

February 21, 2007 Comment on this post [14] Posted in Ruby | Speaking | TechEd
Sponsored By

 It's hard to decide what to spend one's training dollars on. It's hard to justify spending US$2000 or more on a conference. If a conference is nearby, or hosted in your town you can save money. I also use my frequent flyer miles a lot to get where I need to go for conferences. Using your own frequent flyer miles and doubling-up/sharing hotel rooms with friends in the Blogosphere are good ways to justify the financial part of your trip to your boss.

Many feel that it's the company's responsibility to pay for everything, travel, attendance, hotel, etc, but if you want to get as broad a view as possible, and maybe attend multiple conferences, being flexible on how you get there, eat, and sleep can make a difference. Also, trying to go to conferences that happen on the weekend, and making sure your boss knows that he/she's not going to lose you for an entire week - perhaps just a few days - can make a difference.

I'm also careful not to think of conferences as vacations, as you're being paid to absorb as much as you can, so I tend to fly in, attend, and fly out, fairly aggressively, unless my wife and son are along and we have explicitly turned it into a Vacation.

  • I'll be at RailsConf, partially because it's here in Portland, partially because my Boss is a RailsHead, and partially because I think that the mantra of Convention over Configuration is an important one that can be applied regardless of language or environment.
    • Cleverly, this conference is a Thurs-Sun deal, so while it takes up a weekend, it only takes up two work days. Again, a way to get virtually a week's content while only encroaching on work for two days.
  • This year, I'll be going to MIX - a User Experience conference in Vegas. There's an early bird discount if you register before March 15th, so the conference itself is $995. If you're going to Mix, let's meet and have a Diet Soda, eh?
    • Mix is a short conference, but very dense in content, and because it's in Vegas the flights are cheap. Plus, because it's a three-day conference you could go and still work 2 days, or possible that following Saturday and get a good work week in as well.
    • Here's some gravy - every conference attendee gets a free copy of Windows Vista Ultimate (this qualifies for the Vista Family Discount, so you can get two more Home Premiums for $49 each, so that's potentially three copies of Vista for $100, or just keep the Ultimate for free).
  • I'll also be at TechEd 2007 giving a pre-conference with Ron Jacobs (of ARC Cast fame) on Architecture. This is the same pre-con we did in Europe last year. I may also do a session on Mobile applications and AJAX support in PocketIE, but that's still up in the air.

Hopefully I'll see you at one of these conferences!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Reflector 5 Released - World Domination Assured

February 20, 2007 Comment on this post [20] Posted in Tools
Sponsored By

UPDATE: Fixed misspelled title, the permalink for this page changed, fix your incoming links!

Reflector 5 is out, and it is good. Go get it immediately, or just run your current version and go Help | Check for Updates.

What's the coolest new feature? Why, a custom "protocol handler" that will soon sweep the (.NET) blogosphere. Select any item in Reflector and press Ctrl-Alt-C to copy the code:// URI for that item. Like this:

Click here if you have Reflector 5 to navigate to this method: code://System.Xml/System.Xml.XmlNameTable/Add

Note that URI can include the version and hash value for the assembly, as my example does in the underlying href.

code://System.Xml:2.0.0.0:b77a5c561934e089/
System.Xml.XmlNameTable/Add(String):String

Worried it won't work in FireFox? Remember that Custom Protocol Handlers aren't an IE only thing. Notice my FireFox dialog below. Just click "Remember my choice" and you'll have the same glorious integrated experience as everyone else. This code:// thing could take off...I wonder if we can work out some kind of cross-promotional thing with the Java guys? ;)

Here's a few ACTION ITEMS for you after you've downloaded the new version:

  • FONT SIZE: Be sure to visit View | Options and set up your fonts if you use Reflector for presenting. Set your fonts before you present, or call Reflector /fontsize:14 when you start it for a talk.
  • REGISTER WITH EXPLORER: Run "Reflector /register" if you want "Browse with .NET Reflector" to be your default option for all .DLL files.
  • ASSEMBLY LISTS: After you run Reflector, press Ctrl-L and bask in the little-known but totally awesome Assembly Lists feature. I use this for different projects as it let's me "switch views" very quickly. Setup as many lists as you like.
    • Remember that if your list gets "sloppy," just hold down delete and quickly remove every assembly. Then hit Ctrl-L and enter, and you'll be able to select a new Default list and be prompted for a version of the .NET Framework, including Mono if you have it installed.
  • EXPLOIT ANALYZE: Try the new improved Analyze feature by right-clicking any type or Assembly or by pressing Ctrl-R. The new items are Exposed By, Instantiated By, and Assigned By. Exposed By is VERY useful.
    • Also, not new, but still great, is the P/Invoke Imports analysis, that only appears when you've selected Analyze while an assembly (not a namespace) is selected. Try selecting System.Web, Right-Click, Analyze, then P/Invoke and see what unmanaged code your "pure" .NET application is using.
  • GET ALL THE ADD-INS: Finally, go load up on Reflector Add-Ins at CodePlexSo many people use Reflector, but it's profoundly a bummer that so few really exploit the available add-ins. Try the Diff add-in and the TreeMap to start, and once you're hooked, collect them all and trade them with your friends.
  • PUT IT IN YOUR PATH: Oh, and one other thing, folks. Forgive me for this rant, but if I go to another person's machine to debug or work with you for some reason, and Reflector.exe isn't in your path, I'm going to smack someone. Please, let me at least Start|Run|Reflector. If it's in c:\utils\tools\bar\my downloads\foo\reflector, fine, but for the love of .NET, make sure it's in your PATH.

Thanks Lutz! We all owe you.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Coding4Fun: Microbric Viper Robot with an Iguanaworks IR Serial Port and PowerShell

February 20, 2007 Comment on this post [2] Posted in Coding4Fun | PowerShell
Sponsored By

UPDATE: Speaking of Robots, also check out what Ashish is doing with a Laser Pointer and Remote Controlled Car - he's created an autonomous robot!

I finally got around to creating Part 2 of my article on controlling a Microbric Viper Robot with an Iguanaworks IR Serial Port. This time I used Lee Holmes (with permission) LOGO in PowerShell sample and extended it to control the robot. A video is at left hosted on SoapBox and also hosted at Channel9.

Purchasing these Parts

The Microbric Viper can be ordered online in North America, check out www.microbric.com for North American distributors. It's only US$89 at Saelig and CAD$99 at RobotShop. They have a number of educational robots that can be assembled by kids of all ages and skill levels. They're great for the classroom, and include projects like Sumo Robots, and a line-following bot, as well as a Spiderbot that climbs rope - all from the same kit.

You can order the IR Transmitter/Receiver from IguanaWorks. The serial version works on Windows or Linux, and there's a Linux USB version. It's not just a Transmitter, but also a learning receiver that works with WinLIRC and turns your computer into a learning remote control and can be used for nearly any project that utilizes IR

Robotics Studio

A number of folks asked why I didn't use the Microsoft Robotics Studio to do this project. Well, here's my reasoning:

  • I'm ignorant about what the Robotics Studio can do.
  • Early CTPs of the Studio - before it was released - seemed really confusing to me, very abstract and generally obstuse. Not obtuse on the BAD way, just in the "I didn't immediately get it with a few hours and gave up" way.

If anyone thinks that this project and the Microbric Robot could really benefit from the Robotics Studio - or even if you're on the Studio team - do contact me and educate me. I'd love to do a podcast on the Studio, but I don't want to talk about a topic I know so little about. School me!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.