Image of the glider from the Game of Life by John Conway
Skip to content

WordPress, URL Rewrite and Apache2

Okay, this is something that I have been spending the last 3 hours doing. Needless to say, I was getting quite frustrated. However, I have figured it out, and I share my experience here.

WordPress has the ability to take full advantage of Apache's mod_rewrite module. Using your .htaccess file crammed to the brim with regular expressions, you can modify the URL in your address bar dynamically. A URL in your address that looks like:

1
http://www.pthree.org:8080/?p=6

can be rewritten to look something like:

1
http://www.pthree.org:8080/2005/05/19/How_PHP_Works

However, WordPress lacks fundamental detailed documentation concerning the mod_rewrite matter, and Googling around for hours just took me in circles. The only thing that came to mind was common sense. In other words, I was making much more difficult than it had to be.

Here's how to properly configure the mod_rewrite module with WordPress 1.5 using Apache2:

First, make sure that the root of your blogs directory has writeable access. If the root directory of your blog is /home/user/public_html, make sure that public_html is writeable by everyone (chmod 777). Don't worry, you can change it back to something more secure when we finish. Once your directory is writeable by everyone, open your blogs admin page, navigate to permalinks under URLs, and configure the look of your new dynamic URL in the text box, then save your settings. After saving your settings, if you are told you now need to edit your .htaccess file, then you didn't make the root directory writeable. Try again. For convience, I have included the contents of what your .htaccess file should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=50]
RewriteRule ^(welcome)/trackback/?$ /index.php?pagename=$1&tb=1 [QSA,L]
RewriteRule ^(welcome)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(welcome)/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(welcome)/page/?([0-9]{1,})/?$ /index.php?pagename=$1&paged=$2 [QSA,L]
RewriteRule ^(welcome)(/[0-9]+)?/?$ /index.php?pagename=$1&page=$2 [QSA,L]
RewriteRule ^(author-colophon)/trackback/?$ /index.php?pagename=$1&tb=1 [QSA,L]
RewriteRule ^(author-colophon)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(author-colophon)/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(author-colophon)/page/?([0-9]{1,})/?$ /index.php?pagename=$1&paged=$2 [QSA,L]
RewriteRule ^(author-colophon)(/[0-9]+)?/?$ /index.php?pagename=$1&page=$2 [QSA,L]
RewriteRule ^(blogroll)/trackback/?$ /index.php?pagename=$1&tb=1 [QSA,L]
RewriteRule ^(blogroll)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(blogroll)/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(blogroll)/page/?([0-9]{1,})/?$ /index.php?pagename=$1&paged=$2 [QSA,L]
RewriteRule ^(blogroll)(/[0-9]+)?/?$ /index.php?pagename=$1&page=$2 [QSA,L]
RewriteRule ^feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1 [QSA,L]
RewriteRule ^(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1 [QSA,L]
RewriteRule ^page/?([0-9]{1,})/?$ /index.php?&paged=$1 [QSA,L]
RewriteRule ^comments/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1&withcomments=1 [QSA,L]
RewriteRule ^comments/(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1&withcomments=1 [QSA,L]
RewriteRule ^comments/page/?([0-9]{1,})/?$ /index.php?&paged=$1 [QSA,L]
RewriteRule ^search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?s=$1&feed=$2 [QSA,L]
RewriteRule ^search/(.+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?s=$1&feed=$2 [QSA,L]
RewriteRule ^search/(.+)/page/?([0-9]{1,})/?$ /index.php?s=$1&paged=$2 [QSA,L]
RewriteRule ^search/(.+)/?$ /index.php?s=$1 [QSA,L]
RewriteRule ^category/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?category_name=$1&feed=$2 [QSA,L]
RewriteRule ^category/(.+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?category_name=$1&feed=$2 [QSA,L]
RewriteRule ^category/(.+)/page/?([0-9]{1,})/?$ /index.php?category_name=$1&paged=$2 [QSA,L]
RewriteRule ^category/(.+)/?$ /index.php?category_name=$1 [QSA,L]
RewriteRule ^author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?author_name=$1&feed=$2 [QSA,L]
RewriteRule ^author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?author_name=$1&feed=$2 [QSA,L]
RewriteRule ^author/([^/]+)/page/?([0-9]{1,})/?$ /index.php?author_name=$1&paged=$2 [QSA,L]
RewriteRule ^author/([^/]+)/?$ /index.php?author_name=$1 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&day=$3&feed=$4 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&day=$3&feed=$4 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$ /index.php?year=$1&monthnum=$2&day=$3&paged=$4 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$ /index.php?year=$1&monthnum=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&feed=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&feed=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$ /index.php?year=$1&monthnum=$2&paged=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/?$ /index.php?year=$1&monthnum=$2 [QSA,L]
RewriteRule ^([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&feed=$2 [QSA,L]
RewriteRule ^([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&feed=$2 [QSA,L]
RewriteRule ^([0-9]{4})/page/?([0-9]{1,})/?$ /index.php?year=$1&paged=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /index.php?year=$1 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/trackback/?$ /index.php?year=$1&monthnum=$2&day=$3&name=$4&tb=1 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/page/?([0-9]{1,})/?$ /index.php?year=$1&monthnum=$2&day=$3&name=$4&paged=$5 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$ /index.php?year=$1&monthnum=$2&day=$3&name=$4&page=$5 [QSA,L]
</IfModule>
# END WordPress

Realize, this "How'To" is only for Apache2. For Apache 1.3 or earlier, there is loads and loads of documentation on the web. Google around for it.

Now that your .htaccess file is created and edited, you need to edit your default file found in /etc/apache2/. You should be familiar with this file, as you needed to edit it to set up your website on a Linux server. Under the proper virtual host, add:

1
AccessFileName .htaccess

Then, you will need to allow access to files in the document's root directory. This is done by:

1
AllowOverride All

That's it! Believe it or not, it is fairly striaght forward, and quite easy. Again, I was making it way too difficult trying to edit my apache2.conf, httpd.conf, php.ini and a number of other files to get it working, not even thinking about my default virtual directory file.

A list of permalinks for you WordPress blog can be found at http://codex.wordpress.org/Using_Permalinks.

Post a Comment

Your email is never published nor shared.

Switch to our mobile site