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.

0 comments:

Post a Comment