Creating Vanity URLs in PHP: Rewrite Rules

php

Vanity URLs can be extremely useful, especially in offline marketing materials. I covered how to create a subfolder with a header redirect in my last post; that’s the easiest way to get a vanity URL up and running on a web server running PHP. But if you want more than just a handful of vanity URLs, this technique can quickly clutter up the main folder of your  website. In this post, I’ll show you a more advanced technique that will keep your folder structure clean: rewrite rules in Apache.

Apache is the web server software most commonly used to run PHP sites. The web server software handles the request for a specific web page, figures out which PHP file to run to generate the HTML, and then takes care of sending that HTML back to the browser. By default, when someone requests a web address like randyhoyt.com/wcsf, Apache will load a default file like index.php or display a directory listing. With a rewrite rule in Apache, though, we can change how Apache handles a web address.

The easiest way to create a rewrite rule is to put an htaccess file in the main folder of your website. (Depending on your server configuration, that folder might be called public_html, www, htdocs, or something similar: it’s the folder on the web server where the home page file is located.) This file should be named .htaccess. Notice that the file doesn’t exactly have a file name: it starts with a dot, and then it has htaccess for the extension. The H and the T in htaccess stand for hypertext, and you set various web server configuration values here.

Your web server must have the software for rewrite rules enabled. It should: I haven’t run into a shared host that doesn’t. (If you have any trouble getting rewrite rules enabled, please leave a comment on this post.) With rewrite rules enabled, I can turn them on for my site with this command:

RewriteEngine On

htaccess commands are not written in PHP; they use a different syntax altogether. For example, you don’t put a semicolon at the end of a command: you only need a hard return between commands in an htaccess file.

After rewrite rules are turned on, I can create a rewrite rule:

RewriteEngine On
RewriteRule ^wcsf[/]*$ /wordpress/subordinate-post-types/ [R=301,L]

This rule has four separate pieces, all separated by spaces. Let me walk through them, piece by piece:

RewriteRule
Rewrite rules start with this command, indicating to Apache that what follows is a rewrite rule.

^wcsf[/]*$
After the RewriteRule command, the next piece identifies what web addresses should be rewritten by the rule. It uses a syntax called Regular Expressions (commonly called “regex”) to determine if the requested URL matches a particular pattern. I won’t cover regular expressions in depth here — just enough to get our rewrite rule working. This pattern matches any web address that has nothing in between the beginning and the end besides the characters w, c, s, and f (in that order), followed by an optional slash. This pattern will match either of the following two web addresses:

  • http://randyhoyt.com/wcsf
  • http://randyhoyt.com/wcsf/

/wordpress/subordinate-post-types/
After the pattern describing the web addresses to rewrite, the next pieces defines how those addresses should be rewritten. Instead of looking in a subfolder for a file called index.php, Apache will now look at the path specified in the rewrite rule.

[R=301,L]
The last piece contains optional flags that tell Apache how to apply the rewrite rule. This rule has two flags in place, separated by a comma:

R=301
The [R] flag tells Apache to redirect from the original web address to the new web address. (Rewrite rules often change what file Apache sends back to the browser, without changing what gets displayed in the address bar.) The [R=301] tells Apache to send back a 301 response code with the redirect, which we looked at last time.

L
The [L] flag tells Apache that this is the last rule it should apply before redirecting. I don’t want Apache to keep looking at the rewritten address to see if it should apply other rules; I want it to go ahead and do the redirect.

Vanity URLs can be extremely useful, especially in offline marketing materials. Setting up rewrite rules in Apache may be a little more complicated than using a subfolder and a header redirect, but I would definitely recommend this approach if you have multiple URLs you need to redirect.

Randy Hoyt

Randy Hoyt is on the Teaching team at Treehouse. He's been building web sites and web applications for years, and you'll often find him speaking about his experience at local events and conferences. He spends whatever time he can find offline studying mythology and playing board games. Twitter: @randyhoyt

Comments

3 comments on “Creating Vanity URLs in PHP: Rewrite Rules

  1. .htaccess isn’t the extension for the file name. Dot notation is a Unix method of creating a hidden file, so in reality, what you have isn’t a file with no name and an htaccess extension, but a hidden file call htaccess that has no extension.