Month: January 2011

How to give a WordPress Page a memorable address using Apache’s RewriteRules

Alright so this is pretty awesome:

Background:
We have just created one or two new WordPress pages using wp-admin – say an “About” page and a “Resume” page. You’ve done all the work to write up all the content, and format it properly and when you publish, the pages look fantastic!

Problem:
The About and Resume pages have web addresses that look like this: http://www.cabanalabs.com?page_id=99
This is all well and good, but no one is going to remember that our resume is sitting at page_id=99. It would be much more desirable if someone could just type in: http://www.cabanalabs.com/resume

A simple solution, if you are running a newer version of Apache as your web server:
Assuming that we have administrative access to our apache server, and know where our VirtualHost file is, possibly in the /etc/apache2/sites-available/ directory. If you don’t – then stop right here and get a hold of your system administrator! He may have a better solution to this than what you see here :-)..

Modify the VirtualHost for our blog’s domain so that it would include a RewriteRule that would allow someone to then go to the simpler address, http://www.seevishal.com/resume, and reroute them to the correct WordPress page, which in this case would be http://www.seevishal.com?page_id99.

Here’s what the VirtualHost file for seevishal.com looks like:

        ServerName www.cabanalabs.com
        ServerAlias cabanalabs.com *.cabanalabs.com
        DocumentRoot /home/cabanalabs/public_html/
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
        ServerSignature On
        RewriteEngine on
        RewriteRule ^/resume(.*) http://www.cabanalabs.com?page_id=99$1 [L]
        RewriteRule ^/about(.*) http://www.cabanalabs.com?page_id=116$1 [L]

As you can see, we’ve added three lines towards the bottom. One to turn on the functionality, and two with the RewriteRule settings.

For more reading and examples on how to use RewriteRules, check out
Apache’s Mod Rewrite guide