Coding mod_rewrite and WordPress Permalinks

URL rewriting is what mod_rewrite does.

It's an engine that helps redirect a visitor that comes for one URL to another page on the same site or another server. If you are using WordPress with some friendly-looking URLs, you are using mod_rewrite.

Thankfully, WordPress does most of the heavy lifting with pre-built rewrite rules because the syntax is a little unfriendly to the beginner: It combines all you hate about regular expressions with all you hate about having your brain stabbed by rusted forks.

My Problem and Need for mod_rewrite

I’ve been moving from my old domain, ChristopherSchmitt.com, to this one, Christopher.org.

I still have ChristopherSchmitt.com "live" on a server as I work on finishing some migration tasks.

Visitors might stumble onto pages on the old site and stay there. Instead of that happening, I'd like visitors that do come through the old site go to the new site and read the content there.

That means working with mod_rewrite!1

First mod_rewrite Workaround

If my new domain's URL structure was an exact mirror of my original site, I could set up a simple redirect from the old domain to the new domain.

For example, if I had a URL for my old site:

http://christopherschmitt.com/2015/07/21/i-cant-complain/

And new URL for the new domain is:

http://<strong style="background: yellow;">christopher.org</strong>/2015/07/21/i-cant-complain/

The mod_rewrite is relatively straightforward to code up.

To do that, I add the mod_rewrite rule in the .htaccess file which is in your site’s root directory — and is two lines of code:


RewriteEngine On
RewriteRule ^(.*)$ http://christopher.org/$1 [R=301,L]

The first line tells the server to turn on rewriting engine.

The second line does the heavy lifting: it gives the server information on how to process a request from the browser, i.e. the incoming URL, and what do with it, i.e. form a new URL.

When that is added, the links will redirect — however, my problem is a bit different than that.

Little More Complex mod_rewrite Examples

While moving content to Christopher.org, I want URLs without the date. I changed WordPress Permalinks settings to remove the date in the URL:

http://christopher.org/i-cant-complain/

Selecting permalink format in WordPress

That result of this decision means my URL structure for my new domain does not match up perfectly with the old domain.

OLD URL: http://christopherschmitt.com/2015/07/21/i-cant-complain/
NEW URL: http://christopher.org/i-cant-complain/

So, the rewrite needs to be updated to reflect that.

To do that, I would have to use mod_rewrite to grab only the bit of the old URL I need. Thanks to Adam Norword for helping me out with the rewrite rule and sparing me from the regular expression hell I was in:


RewriteEngine On
RewriteRule ([^\/]+)\/$ http://christopher.org/$1/ [R=301,L]

Let's look at the lines to see what is going on:

  1. The first line only has RewriteEngine On and it turns on the rewriting engine in the server.
  2. The second line starts off with RewriteRule and says we are going to have a rule on how the server should handle incoming links.
  3. ([^\/]+)\/$ tells the server to grab just the slug title of the page or post by grabbing the end portion of the URL.
  4. http://christopher.org/$1/ - this tells the server to grab that title of the page or post and append it to the new domain to create the new URL
  5. [R=301,L] this section tells the browser to signify the code is a 301 direct. Using 301 redirects is a method recommended by Google to let them update their listings.

Further Reading on mod_rewrite and WordPress Permalinks

At one time, I was thinking of diving into WordPress to update the Permalinks to add the redirection I needed. At a recent Austin WordPress meetup, I saw a great presentation on Advanced Permalinks in WordPress by Pete Nelson that shed some light on such an approach.

While I didn't go that route, his presentation did let me know about some great WordPress resources:

And Adam showed me Regular Expression 101. It works as a regular expression decoder: taking regular expression formulas and decoding them into human-friendly language.


1. If it was one off page, I would use meta HTML element with refresh.

About Christopher Schmitt

The Internet's Christopher Schmitt is an award winning designer, author, and speaker and one of the people behind the web conference team, Environments for Humans. He hosts the Non Breaking Space Show and curates the weekly UX Design Newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *