Text Size
   
Home Tutorials URL Rewriting in IIS
Jul 04
Saturday

What's new in StudioAMK.com!

  • Welcome Back !!!

    heartbeatW

    elcome Back!!! The New StudioAMK.com is coming back online with more and more interesting and exicting features. As usual, here you can get various softwares, games, multimedia files and various kind of articles at no cost, which are originally created by me...
  • New Structure...

    blog-creation-4

    S

    tudioAMK.com has been restructured to have lesser sections. It has been narrowed down to as little as two sections, Blog, where all my articles go and, Creations, where all my original creations such as programs and files go...
  • New Login Systems...

    keyD

    id you know... You are already a registered member of StudioAMK.com, if you have a account or an OpenID account. A wide options of authentication creates an easy way to use a single digital identity across the Internet. It doesn't matter, if you do not have such accounts. You can always register a new account at StudioAMK.com.
  • More & More Features...

    bookmark Social Bookmarkings textsize Adjustable Font Size
    tags Tags Cloud pdf Article to PDF
    notify Follow-up Notification print Easy Print
    search Advanced Search
    mail Email to Friend
  • Download Font

Add to: JBookmarks Add to: Facebook Add to: Mr. Wong Add to: Windows Live Add to: Bookmarks.cc Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: Jumptags Add to: Upchuckr Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Spurl Add to: Google Add to: Blinklist Information

Log In



You can also Login with a account or
URL Rewriting in IIS PDF Print E-mail

Tags: IIRF | IIS | PHP | URL Rewrting

Monday, 01 December 2008 19:30
The first question is; what exactly is URL Rewriting…? Is that the case when you go to http://studioamk.com and you are brought to http://studioamk.com/blog? NO! This is URL Redirecting and not URL Rewriting! Let us start with an example.

The following URL contains a query string parameter (title) for getting a particular blog post.
http://studioamk.com/blog/getpost.php?title=PageTitle

This can be altered to make it more user and search engine friendly URL as follow.
http://studioamk.com/blog/PageTitle

In another word, when you go to http://studioamk.com/blog/HelloWorld, you will at the same page as you entered in address bar of your browser, but at the backend, the web server will work as http://studioamk.com/blog/index.php?title=HelloWorld and will bring contents from that page to you. This is called URL Rewriting and this can be done by the use of a rewrite engine, which is a software that modifies web URL's appearances.

We will use Ionics Isapi Rewrite Filter (IIRF) to implement URL Rewriting feature in IIS, which is an ISAPI filter, similar in functionality to apache mod_rewrite, that dynamically alters a URL based on rules defined in the configuration file. It is an open-sourced software and we can download and use it for Free.

Step-by-step Guide
  • First, go to IIRF's CodePlex Page and grab the latest application version of it (not source code version!). In this tutorial, we are going to use version: 1.2.15.
  • Extract the files to a folder.
  • Now, go to IIS and Right-Click on the website or virtual directory where you want to implement URL rewriting and choose Properties.

01

  • Go to ISAPI Filters tab of the properties window, click on Add button. Then, Filter Properties window will popup. In the Filter Name field, fill in whatever you want (I filled mine as "URL_Rewrite"). Then, click Browse and select IsapiRewrite4.dll form lib folder of the extracted folder.

02

  • Now you can click on OK button to close the Filter Properties window and Apply Button on the Site Properties window. We are done adding rewriter engine in IIS. We’re now going to define a rewrite rule to test out our installation.
  • In this tutorial, we’ll just define a very simple rewrite rule; "all values after our domain name will pass to the title parameter of a file name called rewrite_test.php". For example, studioamk.com/HelloWorld will be rewritten into studioamk.com/rewrite_test.php?title=HelloWorld.
  • Normally, rewrite rules in configuration file are defined using Regular Expression (Regex). To get everything after domain as we defined in previous step, we will just capture everything after the first appearance of a slash character, which can be written in Regex as " ^/(.*)$ ".
  • ^ " and " $ " are Start and End symbols. " /(.*) " means everything after a splash. To get values in " (.*) ", we can use $1 variable as we’ve already grouped them by the use of brackets. Then, we’ll pass what we’ve captured, which is already stored in variable $1, to title parameter of the php file.

03

  • After we’ve logically defined the rule or the business logic, we are now going to write it in the configuration file. Go back to the folder where IsapiRewrite4.dll located and create a text file. Then, rename it to IsapiRewrite4.ini. Open the newly created ini file with notepad or any text editor of your choice. Copy and Paste the following line in your ini file.
RewriteRule ^/(.*)$ /rewrite_test.php?title=$1 [I,L]
  • As in above rule, "[I,L]" are called modifiers. "[I]" means our rule is case-Insensitive. "[L]" means this rule is the Last rule. In another word, if the user request matches one of our last rules’ pattern, the rewriter will stop after that rule and will not go for remaining rewrite rules (if any).
  • Now, we need to work on our php file. Copy and Paste the following code to rewrite_test.php and place it under website or virtual directory where you setup the URL rewriter. Basically, it will display the values store in title parameter.
  $Page = $_GET["title"];
echo "It works! You were looking for $Page.";
?>
  • Now try browsing your site. Any URL you browse will rewrite to rewrite_test.php. You may need to restart IIS service, if rewriter is not loaded in IIS.
Thus, as you observed, defining RewriteRule is the key in URL Rewriting. Without knowing it, you can’t achieve anything. Therefore, please allocate some times to read through the Readme file that comes along with IIRF zip file. If you are already an expert in Regex, it will be just as easy as counting 1-2-3. Alright, that’s it for today and… Good Luck!
 

Comments  

 
#1 Carlos 2009-02-11 13:00 Thx… It works Quote
 

Add comment


Security code
Refresh

Home Tutorials URL Rewriting in IIS