This course teaches developers (and administrators) how to use URL Rewrite for URL and HTTP Header manipulation. You will learn the fundamentals of URL Rewrite, how to do whatever you dream with URLs, and how to work with powerful rules using Regular Expressions. This course will help you create rules for SEO purposes, site redesigns, creative reverse proxy tricks, merging two (or more) sites into a single site, using rewrite maps, and more. We will also look at when to use URL Rewrite vs. using rewriting within MVC or WebForms.
Scott Forsyth is co-founder and chief systems architect of Vaasnet, and a consultant for the web platform on the Microsoft technology stack. Scott is a 10-year Microsoft MVP for IIS/ASP.NET, an ASP Insider, has co-authored two Wrox books on IIS, and speaks at code camps and technical conferences.
Section Introduction Transcripts
Section Introduction Transcripts
URL Rewrite for Developers Hello, my name is Scott Forsyth from Pluralsight. In this course, we will look at URL Rewrite for IIS, which enables powerful rewriting and redirecting of URLs, server variables, and even page content. After you've completed this course, you should be able to create your own rules, work with URL parts, edit reasonably complex regular expression rules, optimize your site with common search engine optimization rules, and basically do some pretty cool stuff with incoming and outgoing URLs. This course is for you, whether you're new to URL Rewrite or if you already have a working knowledge and would like to understand it better. This course is targeted for developers and that I assume that you know development really well, but you may not work with IIS on a day-to-day basis. So I'm going to cover a few different things that will help you in the IIS space as well, but if you're an administrator, come along for the ride, there's a lot that you will be able to gain from this as well. This course is made up of seven modules, the first of which we're in right now, an Introduction to URL Rewrite. Then we're going to take a look at some rule templates built into URL Rewrite. We're going to take a look at understanding the various URL parts that will really help in creating rules. We'll have some fun in creating regular expressions and trying to understand the regular expressions that matter for URL Rewrite. We're going to take a look at some common rules that you'll probably be faced with on a day-to-day basis. Then we're going to cover some further considerations that aren't covered elsewhere in the course that I believe are beneficial to you. And finally, we're going to look at some troubleshooting tips and tricks that should help you when you try to troubleshoot what doesn't work the first time when you hope it should.
Rule Templates The URL Rewrite tool in IIS Manager provides a number of rule templates. Many of these templates are in the form of a wizard and they make it easy to get started with a few of the common rule types. And note that these generator rules are just a starting point enabling you to create a rule from the wizard and then you can make any changes that you want. In this module, we will first take a more detailed look at the URL Rewrite tool, then we'll take a look at the rule templates available to us. As we look through each of these rule templates, we'll look first at what the rules are and then we'll discuss any considerations that we should be aware of. In this screenshot, you can see the Add Rule tool available within URL Rewrite. And here's a number of rules that are available to us. Now notice that not all these rules are available all the time. The Reverse Proxy rule is only available if we have Application Request Routing, ARR installed, and two of the rules, the Canonical Domain Name and Append or Remove the Trailing Slash Symbol rule are only available at the site level. You won't see these if you try to do them at the server level. These rule templates are broken up into four different types of rules. First we have Inbound rules. These are rules that impact the incoming requests only. We have Inbound and Outbound rules. These ones are the same as Inbound rules, but they offer some outbound functionality too and we have the User-friendly URL and Reverse Proxy. We also have a Blank rule just to get us started without outbound only rules. And we have a number of search engine optimization rules, specifically just to optimize our site for search engine optimization.
URL Parts and Other Useful Server Variables To really understand URL Rewrite, you must understand the URL parts and the server variables used in the conditions and actions of the rules. There are dozens of server variables available to us, making it easy to get lost in a sea of variables. The server variables aren't given friendly names like domain name or file name and path, so we have to figure out which one to use in each situation. In this module, we will take a look at the parts of the HTTP URL and discover the corresponding URL Rewrite server variables. We will look at ways that you can discover server variables and HTTP headers for yourself and we will look at HTTP headers specifically and how to convert the name of an HTTP header to the corresponding URL Rewrite server variable. Once you've completed this module, you should be familiar with the variables necessary to create all types of URL Rewrite rules. Now let me spend a brief moment talking about the terminology used in this module. A server variable is the blanket term used for all types of variables that IIS makes available to URL Rewrite, and even makes available to programming frameworks like ASP. NET. Server variables also include HTTP headers. HTTP headers are a specific type of server variable that are sent from the client, usually a web browser. They include the host header, user agent, and whether a browser supports compression. In URL Rewrite, for each visit from a visitor or a spider or tool, you have available at your fingertips information about the URL, HTTP header information, and other IIS server data. Together these make up the server variables that you can use when creating your rules.
Common URL Rewrite Rules In this module, we will take a look at eight common URL Rewrite rules. These rules will lay the foundation for a good majority of rules that you'll be faced with. The rules that we will create will include an HTTP_PROTOCOL option, which will maintain the HTTP, or HTTPS on a redirect. We'll create a rule that will redirect the domain name to or from www while maintaining the original domain name, so it supports multiple domain names. We will look at redirecting a subdomain, or third level domain into a subfolder, and we'll take that further and look at how to host multiple domain names under a single site. We'll spend a bit of time on this one and look at four different rules that are required to make this run really well. Then we'll look at three options for a down for maintenance page and see the pros and cons of each. We'll look at creating some rules for ARR load balancing since URL Rewrite and ARR work together so well. Then we'll look at a rule that prevents image hot-linking so that other people can't steal our images or bandwidth. And finally, we'll look at some HTTP to HTTPS rules along with some considerations that we need to be mindful of when working with SSL.
Further Considerations In this module, we will look at three topics which haven't been covered yet, or have been covered, but need more attention. The first that we'll look at is Outbound rules and compression. If you have dynamic compression enabled for a site and you create an Outbound rule, the site will start failing with a 500. 52 error. We'll look at how to address this. I'll also discuss other aspects of Outbound rules, which will help you with creating your own rules. Then we will look at a few ways to optimize your sites for maximum performance, both Inbound and Outbound rules have ways in which they can be optimized. We'll also look at output caching to see how you can inadvertently invalidate kernel-level caching. Finally, we will look at rewrite maps further to see how they can be used for simple lists rather than key value pairs and we'll look at how to perform and compare against a part of a string.
Troubleshooting URL Rewrite In this module, we will take a look at tips and tricks for troubleshooting unruly URL Rewrite rules. We'll also look at how to tell how long it takes to execute each rule so that you can confirm the performance is well optimized. We'll start by looking at failed request tracing, FRT for short. URL Rewrite drops pretty much all details into FRT so you can tell exactly how a rule is or isn't executed. Then we will look at creating a breaking rule that you can create and filter for just yourself. This becomes a tool that you can use for certain types of troubleshooting. Next, we will look at a way to find out for sure what a particular variable's value, or back reference is, from URL Rewrite's perspective. Finally, I'll step through a few additional troubleshooting ideas. Some are new thoughts for the course and others are reminders of useful tools, which have been discussed already throughout the course.