How to create a url alias using IIS URL Rewrite



Want to allow someone to shortcut their way to a particular part of a website? Jonathan Yong, one of our Premier Field Engineers from Malaysia, shows you how!


Problem: I have a url or domain that is very long! How can I create a shorter url that acts as an alias, which maps to a subfolder under the long url?

Example: Instead of going to www.verylongurl.com/folder, I want people to simply go to www.shorturl.com.

Answer: You can use the IIS URL Rewrite module to create a rule which maps one url to another.

Prerequisites: This solution only works with IIS 7 and above.

Details: In the following, I will show you step by step how to accomplish this. There’s also a sample file included (see the download link at the bottom of this page) so that you can try this in your own environment.

1.

Download and install the IIS URL Rewrite module from IIS.net.

If IIS Manager is running when you install URL Rewrite, you will need to close and reopen it in order to see the URL Rewrite configuration tool icon.

image

2.

Next, I will copy the files and folder necessary to set up my web site.

  • Extract the sample code
  • Open an elevated command prompt (Start, CMD, Ctrl+Shift+Enter, Yes)
  • Go to the extracted sample code folder in the command prompt.
  • Run 01-copy_file.bat. This will copy the sample files to c:\inetpub\verylongurl folder.

Don’t close the command prompt yet. We still need it for subsequent steps.

image

3.

I will create a web site which maps to the folder above and has two bindings, using host headers.

  • The web site name is ‘Long URL Web Site’.
  • The port number and host header binding is : 80/verylongurl.com and 80/shorturl.com

From the command line, run 02-create_site.bat to create the web site.

image

4.

On the Internet, we’d need to add both hostnames to DNS so that users can surf to the site.

In our test environment, we simply add this to the HOSTS file on any computer we’re testing from.

  • Open a notepad elevated as Administrator (Start, NOTEPAD, Ctrl+Shift+Enter)
  • Open the HOSTS file in C:\Windows\System32\drivers\etc\hosts.
  • Add the following two domain name to the hosts file and save it.

image

5.

We will now create the URL Rewrite rule which maps the short url to the long url.

  • Go back to IIS Manager.
  • Click on the ‘Long URL Web Site
  • Double click the URL Rewrite icon to open the URL Rewrite configuration.

6.

In the URL Rewrite configuration, click on the Add Rule(s) link in the Actions pane on the right hand side.

image

7.

Select the Blank rule template and click OK.

image

8.

In the Edit Inbound Rule screen,

  • Enter “URL Alias” (or any name you like) in the Name textbox,
  • Make sure Requested URL is Matches the Pattern,
  • Make sure Using is Regular Expression,
  • Enter “.*” in the Pattern textbox.

image

9.

Click the down arrow to expand the Conditions title, then click Add.

image

10.

In the Add Condition dialog,

  • Enter {HTTP_HOST} in the Condition input textbox;
  • Select Matches the Pattern in the Check if input string;
  • Enter shorturl.com in the Pattern textbox
  • Click OK.

image

11.

Scroll down further to the Action section.

  • In the Action type, select Rewrite.
  • Enter “/subfolder/{R:0}” in the Rewrite URL.
  • Tick the checkbox on Stop processing of subsequent rules.

image

12.

Click the Apply link on the top right of the Actions pane.

The configuration is saved to the web.config file in your web site folder. You may open this web.config file to review how the configuration is being created under the hood.

image

Let’s review what we just did from step 8 to 11:

  • We create a rule that matches all incoming urls (step 8), and
  • If the host in the url matches “shorturl.com” as specified in the Conditions section (step 9 and 10),
  • Rewrite the url to “/subfolder/<incoming url>” (step 11).

If you browse to http://shorturl.com/abc.htm, the url will rewrite to http://shorturl.com/subfolder/abc.htm.

However, if you browse to http://verylongurl.com/abc.htm, the url will not be rewritten because it does not match the host name we are looking for, which is “shorturl.com”.

To see this in action, browse to the following pair of urls and you should see that each pair will show you the same page.

Test A:

Test B:

You can learn more about URL Rewrite module here.

Download the sample file here:

 


Posted by Tristan “Bridesmaid” Kington, MSPFE Editor

Comments (13)

  1. Balu Kalepu says:

    Hi, Thanks for the great post.

    I have a challenge. Currenlty one of our application is running on Port 80. (Ex:
    http://www.xyz.com/abc)
    But few of the pages are hard coded to use a different port (Ex:
    http://www.xyz.com:1025/abc/123).

    How can we remove the port number 1025 from the URL using URL Rewrite?

    Thanks,
    Balu Kalepu

  2. MY NAME IS: says:

    yuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuTTTTTTTTTTTTTTTTTTt

  3. Jenny says:

    La Empresa se reserva el derecho inviolable a quitar la opción Cómpralo Ya de una
    subasta particular. http://rusticwedge342.blox.pl/2016/10/Subastas-On-line-iquestEstafa-O-Chollo.html

  4. Hello everybody, here every person is sharing these experience, thus it’s nice to read
    this blog, and I used to pay a visit this weblog everyday.

  5. どれだけトイレが頻繁かというと、就寝後は平均して4度ぐらいはトイレに駆け込むくらいなので困ります。

    便所が近くなる要因としてあげられることは、水分を一度に多く飲んでしまうことだと思います。

    喉が乾きやすいから、ペットボトルをカバンに入れているのです。

    いつもにトイレが頭にあることも、原因なのではないかと思われます。

    トイレに関しては小学生ころに大惨事に見舞われてしまい、おとなになっても忘れることができません。

    おへその下に手を置く癖があることも、お手洗いが近くなってしまう要因ではないかと思います

    ついついお腹をてのひらで圧迫してしまう癖があります。早く治ししたいです。

  6. 他人と比較しておしっこが近くなる頻度が多いと思う

    どれだけトイレが近いかというと、深夜は平均して4回ぐらいはトイレに駆け込むくらいなので困ります。

    お手洗いが近くなる原因としてあげられることは、飲み物を一度に多く飲んでしまうことだと思います。

    喉が乾きやすいので、サーモマグを手放せないのです。

    常時にトイレのことを考えていることも、原因なのではないかと思われます。

    トイレに関しては小学生ころにトラブルに見舞われてしまい、現在でも忘れることができません。

    お腹を押す癖があることも、お手洗いが近くなってしまう要因ではないかと思います

    ついつい下っ腹を指で圧迫してしまう癖があります。早く治ししたいです。

  7. Hi there, You have done an excellent job. I’ll definitely digg it and personally suggest to my friends.
    I’m confident they’ll be benefited from this website.

  8. test says:

    Truly no matter if someone doesn’t know then its up to other viewers
    that they will help, so here it takes place.

  9. This is the perfect web site for anyone who really
    wants to understand this topic. You know a whole lot its
    almost hard to argue with you (not that I personally would want to…HaHa).
    You certainly put a new spin on a topic which has
    been discussed for ages. Wonderful stuff, just wonderful!

  10. Method Of Findinng A Roulette Game Online

  11. Brittany says:

    Postcatd Marketing Has Gotten Harder, An Individual Must
    Be Smarter

  12. Jeanna says:

    The listt runs in order to incclude many decisions,
    big and small. Thewre are many fun sign options provided.pick one that works for
    you. Online users are trying to studxy the facts.

  13. Leonardo says:

    Are you looking regarding your option enhance double also triple revenue out oof yourr weblog?
    Getting also be configured goes and delineate specific splaces within the trade sshow space.

Skip to main content