Showing posts with label URLRewriter. Show all posts
Showing posts with label URLRewriter. Show all posts

Thursday, October 6, 2016

URLRewriting in the land of Global.asax

Earlier I wrote about using URLRewriter.net. It's a decent piece of software, but has some serious drawbacks, especially since it forces you to modify the IIS configuration for a specific website. As a webmaster, trying to keep that straight on 1500+ websites isn't exactly an ideal solution. So today, I'm going to present you with a fairly basic method in this case of handling it in the global.asax file. This method is handled inside the Application_BeginRequest Subroutine.


Dim incoming As HttpContext = HttpContext.Current
Dim oldpath As String = incoming.Request.Path.ToLower()
Dim pageid As String
Dim DynamicPageName As String
Dim BareName As String
Dim regex As System.Text.RegularExpressions.Regex
Dim matches As MatchCollection
'------------------------------------------------
' URL rewriter for WebPage.aspx?pageid=xxx
'------------------------------------------------
' set unique page name for dynamic pages in web.config
DynamicPageName = ConfigurationManager.AppSettings("cfgWebPageName")
' get the bare name for moving pageid in the string
BareName = RemoveFileExtension(DynamicPageName)


Here is the RemoveFileExtension function. We can't use the Path.GetFilenamewithoutExtension function because of the potential case for a QueryString at the end of a dynamic page:


Function RemoveFileExtension(ByVal parmFileName as String) as String
   Dim FullLength as Integer = parmFileName.Length
   Dim RightStringToCut as String = parmFileName.Substring(parmFileName.LastIndexOf("."))
   Dim RightLength as Integer = RightStringToCut.Length
   Dim NewStringLength as Integer = FullLength - RightLength
   Dim NewWebPageName as String = parmFileName.Substring(0, NewStringLength)
   Return NewWebPageName
End Function


Now onto the rest of our application...


regex = new System.Text.RegularExpressions.Regex(BareName & "(\d+).aspx", RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
matches = regex.Matches(oldpath)
If matches.Count > 0 Then
   pageid = matches(0).Groups(1).ToString()
   incoming.RewritePath(DynamicPageName & "?pageid=" + pageid)
Else
   incoming.RewritePath(oldpath)
End If


In the above code, it is specifically looking for a page named "webpageNN.aspx" where NN is equal to the page ID. So the page would be labeled as webpage18.aspx. It then rewrites the URL to webpage.aspx?pageid=18. The user, searchengines, etc. still see webpage18.aspx though. Easily enough, this could be expanded to be used with more friendly URLs, such as mywedding.aspx which would rewrite to webpage.aspx?pageid=18 via the addition of database information.

Friday, April 4, 2008

Using mod rewrite in Apache

In a previous post I discussed URL rewriting in .NET. I was recently asked for advice on rewriting URLs in an Apache environment. So below is my answer to that question.

In the .htaccess file we do the following:
RewriteEngine On
#if query sting exists, then append to var2
RewriteCond %{HTTP_HOST} ^myschool\.edu$ [NC] RewriteRule ^(.*)$ http://www.myschool.edu/$1
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)((.(htm|php))|\/)$ pages.php?var1=$1&%1 [L] RewriteRule ^$ home.htm [L]
[R=301,L]

(Entries preceded by # are commented lines of code.)
The first line, RewriteCond is checking to see if the URL is http://myschool.edu/<restofdomain>. In that case we change it to http://www.myschool.edu/<rest of domain>. The R states that this is a forced redirection (thus is updates the URL's browser to display www.myschool.edu instead of just myschool.edu. 301 is the code, meaning permanent redirection. L means last command, stop executing. (When this occurs, it will restart the Rewrite condition upon load.)
The next rewriteCond command is to ensure any pre-existing Query Strings are passed on.
The next Rewrite rule, checks to see if the page includes a .htm, .php, or a forward slash (/).
If so we rewrite the URL as described. The [L] signifies that last command.
Lastly, if it's not that, then that means it's just www.myschool.edu in which case we modify it to be www.myschool.edu/home.htm

Basically we are rewriting the URL behind the scenes to the appropriate page. Here is an example
http://www.myschool.edu/Current-Students.htm is rewritten to http://www.myschool.edu/pages.php?var1=Current-Students
From there, our pages.php code retrieves the appropriate page from our CMS (home-grown in our case) and builds the page based on its settings. (Currently our pages are always built dynamically instead of dynamically.)
However, in the browser it still appears as the original URL.
For more information on mod_rewrite click here.