Showing posts with label vanity urls. Show all posts
Showing posts with label vanity urls. Show all posts

Friday, December 3, 2010

How to handle GoDaddy's preinstalled URL Rewrite when running code locally

This blog assumes that you are either taking advantage of the Microsoft URL Rewrite module on a GoDaddy hosting server or another hosting server that has URL Rewrite preinstalled. If you have questions about how to use this module on GoDaddy, then click here to read one of my previous blogs that goes more into detail on how to implement the module.

If you are using the GoDaddy pre-installed URL Rewrite module on your site, and testing changes locally, you've probably run into a couple problems when you attempt to go to a url utilizing the rewrite method.

For example, if my site has user profiles that can be accessed with a unique ID the user chooses, a profile URL might look like this:
http://www.digitalplaydoh.com/RobFine

Behind the scenes, my URL Rewrite method looks something like this:
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{URL}" pattern=".*.axd$" negate="true"/>
</conditions>
<action type="Rewrite" url="MyProfile.aspx?key={R:0}"/>
</rule>
</rules>
</rewrite>


On GoDaddy's servers, this will execute perfectly however, while I'm debugging the code on my local machine, the URL Rewrite module is not available, meaning I constantly get a Server Error when I try to go to http://www.digitalplaydoh.com/RobFine yet when I go to http://www.digitalplaydoh.com/MyProfile.aspx?key=RobFine everything works fine.

There is a solution!

I have created a function within my Profile class called ProfileURL that I use to return the url for a particular profile. I originally created it to handle cases where users had not chosen a unique ID, but now it's going to help us develop locally as well.

The original function looked something like this:


If there is a unique ID for the user, it will send the unique id back as the url.
If there is no unique ID established, it will send back the profile page MyProfile.aspx and feed in my Profile ID.

It is important that anytime I feed my Profile URL to a link, I call this function.

Now, I need to add a bit to the function to support when I'm debugging locally. In the case of the "RobFine" profile earlier, this profileURL will still return just "~/RobFine" as it stands now. What I can do is add in a simple IF THEN statement to check if we are running locally:
Public ReadOnly Property ProfileURL() As String
Get
If HttpContext.Current.Request.IsLocal Then
Return "~/MyProfile.aspx?id=" & _id
Else
If Not _uniqueID = "" Then
Return "~/" & _uniqueID
Else
Return "~/MyProfile.aspx?id=" & _id
End If
End If
End Get
End Property


HttpContext.Current.Request.IsLocal will return a boolean function if my code is running locally.
If it is running locally, I want to throw back a URL that doesn't implement the URL Rewrite.
If it is not, then I'll have the function act as it normally would on the server.

Monday, June 14, 2010

AJAX and URL Rewrite: Sys is undefined Issue

I created a website on a subfolder of my domain, and later moved it to a subdomain as its own application. Oddly, after I did this, I saw an issue pop up wherein javascript errors were thrown every time I opened a page that:
Message: Syntax error
Line: 3
Char: 1
Code: 0
URI: http://(domain)/WebResource.axd?d=3sB1WgLxUgrovkMyz-aqWw4&t=633626442871064790

... along with 2 errors for the ScriptResource.axd file and 2 instances of a 'Sys' is undefined error.
After some research, I found that this is caused by the combination of AJAX and URL Rewrite. I'm not sure if it is specific to GoDaddy hosting, or global. It's strange that it didn't surface until I moved my project. Regardless, I have found a solution.

The major problem is that my application is not recognizing the WebResource.axd and ScriptResource.axd as files; therefore tying to rewrite their urls using them as keywords.

My current URL Rewrite structure looks like this:
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
</conditions>
<action type="Rewrite" url="UserProfile.aspx?key={R:0}" />
</rule>
</rules>
</rewrite>


I'm already checking for files and directories, but now I need to check for these .axd files before casting a url into the rewriter. Therefore, I'll use the bounce URLs off a pattern *.axd to negate them from being processed by the Rewrite function.
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
<add input="{URL}"
pattern=".*.axd$"
negate="true" />
</conditions>
<action type="Rewrite" url="UserProfile.aspx?key={R:0}" />
</rule>
</rules>
</rewrite>


I re-ran my application and every worked like a charm!

Wednesday, June 2, 2010

Vanity URLs with GoDaddy Hosting using URL Rewrite

GoDaddy is a very affordable web hosting provider and domain registrar. I often recommend them to most of my clients who don't already have hosting set up. Unfortunately, GoDaddy does not allow you to access the IIS panel directly which sometimes makes a simple task slightly more complex as you have to navigate through their hosting control center, or sometimes entirely unfeasible. However, for the most part, they provide you with everything you would need to host most websites for a nominal fee.

I've recently had the need to create a portal that used vanity urls for each user profile on the site. For instance, if Aaron, Bob and Carl all have profiles on the portal, their respective portals may be:
Aaron: digitalplaydoh.com/profile.aspx?id=2
Bob: digitalplaydoh.com/profile.aspx?id=5
Carl: digitalplaydoh.com/profile.aspx?id=31

The client would like users to be able to select a unique ID for their account that can be typed out after the domain that will take users straight to their profile, similar to what you would find on Facebook or MySpace. ie:
Aaron: digitalplaydoh.com/aaronsmith
Bob: digitalplaydoh.com/bigbob
Carl: digitalplaydoh.com/carl


Microsoft's URL Rewrite module comes preinstalled on GoDaddy accounts with IIS 7.0. Here's where things get a little unfortunate for the GoDaddy developer. While running IIS 7.0, you will be unable to use Front Page Extensions, which means if you're using Visual Studio, you'll need to now connect to the site with the FTP method. Not terrible, but definitely a little slower.

Once you're running IIS 7.0, we're ready to start using the Microsoft URL Rewrite module. Open your web.config file and go to the <system.webServer> and add the following:
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
</conditions>
<action type="Rewrite" url="Profile.aspx?key={R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>


This allows us to find any path that is not a file or directory, and redirect it to Profile.aspx?key=(path). This means I need to now make sure that Profile.aspx can support me passing a "key" request parameter in instead of an "id", which is just a little work in how we pull from the database, but we've otherwise now go the ability to go to http://digitalplaydoh.com/carl and get Carl's profile page.

Note that you also need to be ready for an incorrect "key". Should I accidently type http://digitalplaydoh.com/calr, you need to be able to catch this as a incorrect keyword and show a proper error page.