Easily shorten a URL with mod_rewrite. This video gives a brief introduction to this powerful module.
- [Instructor] There's a handy Apache module called mod_rewrite that we can use to change how URL requests work. For example, in this video, we'll create a rewrite rule to make one URL path refer to a different one. Imagine you're a conference speaker and you give a lot of talks, but you're also very organized. You post all of your talk materials in a folder structure on your website, but you want to give out a nice, easy-to-remember link for your audience to download the talk outline. Of course, you could use the same technique for a product page or an affiliate URL.
To start with, here in our HTML folder, let's make a really nice, deep path. I'll write sudo mkdir -p archive/talks/2016/05/27/widgets/widgets-and-you. I'll copy the outline.txt file from the exercise files over there. All right, sudo cp, my home folder, and then, you know what? I'm going to copy and paste this.
Now, we can get to this file by requesting the URL in the browser. Our domain, archive/talks/2016/05/27/widgets/widgets-and-you/outline.txt but that's pretty long, and it would be much easier to give out a short URL at your talk. Rather than using a URL shortener or moving the content to a shorter path, we can use a rewrite rule to accomplish the same effect.
A rewrite rule, at its most basic, matches a pattern in the URL and gives a response. For now, we'll write a basic rule so you can see how they work. I'll create an .htaccess file here in my site root. In it, I'll write two directives. First, RewriteEngine On, which tells the web server to interpret rewrite rules. Changing it to off disregards the rules following it, so that can be a quick way of controlling whether your rules are being enacted. Then, I'll write a line that takes one path and changes it to a different one.
This is our RewriteRule. This will respond to requests for our site/outline with the contents of the outline.txt file with the really deep path here. I chose the name outline as our parameter here to illustrate an important point. The parameter in the first position, the text that we're rewriting is a regular expression, so if I had just put "outline" with quotes around it, the engine would say, okay, I have outline. I know what to do with that. Then, it would go request the long path, all this /outline.txt, and say oh, outline, I know what to do with that and repeat the process.
The web server stops it at 10 redirects, or 10 times through this loop, by default and throws an error. So I need to use the regular expression symbols, caret, to say only look for outline at the very beginning of the string and, just to be safe, $ at the end, saying only match strings with nothing after the parameter here. In this case, I could have meant either, but I want to be very specific. Okay, I'll save this and load it up in my browser. Totallyrealwebsite.com/outline.
There's the content from my file. It hasn't moved or anything. We've just made a shortcut to that content. These redirect parameters do need to be unique, of course, or the server won't know which of many redirects to enforce. Rewrite rules are powerful and can give you a lot of flexibility. You can set up dynamic rules that match user agents, host names, and all kinds of things. I just wanted to give you a quick introduction here, but do check out the documentation for more advanced topics.
- Starting up and shutting down a web service
- Configuring logging on Apache
- Adding modules to extend Apache
- Using virtual hosts
- Securing access with SSL
- Adding an SSL certificate to your site
- Protecting a site with .htaccess files