Breadcrumbs

Mod_Rewrite for Search Engines 101

Applications have to be safe, user friendly, and search engine friendly. Dynamic pages make all three of these issues difficult. Hackers take advantage of the dynamic URL. Search engines ignore the page. Users don't remember the pages. So what do you do?

Apache's mod_rewrite (external link) module allows for URL rewriting. This powerful module allows you to solve all three of these dilemmas.

Search Engines & Dynamic Pages

Dynamic pages functionally allow for passing variables to a script, but they also come with conflicts. One of these problems is search engines, a vital marketing resource to your website. The problem is that search engines ignore the dynamic part of the URL, which is what normally renders the contents of the page.

For example take this URL:

http://www.lowter.com/index.php?id=article&article=210 (external link)

Many search engines will only read up to the dynamic marker or the question mark ("?"). So a search engine might read:

http://www.lowter.com/index.php (external link)

This isn't the same page, so the search engine won't index any of your dynamic content. If your entire website is run dynamically (via a database) then search engines may only index a blank page, or an error message.

How Mod_Rewrite Will Help

Using mod_rewrite you're given the ability to rewrite URLs into a more search engine and user friendly format. Let's take the URL from above:

http://www.lowter.com/index.php?id=article&article=210 (external link)

Using mod_rewrite we're enabled to transpose the URL into something more uniform:

http://www.lowter.com/articles/210 (external link)

Search engines will read this URL as if it were a directory or static page, rather than a dynamically executed web page, indexing all of the content located on the web page.

Enabling Mod_Rewrite

Antecedent to rewriting your URLs, you must enable the Apache module. Open your httpd.conf configuration file. Uncomment the line, by removing the hash (#), reading:

Code: .htaccess

#LoadModule rewrite_module

Repeat this step with the line reading:

Code: .htaccess

#AddModule mod_rewrite.c

Simply restart Apache and mod_rewrite will run. What these two lines do are load the module, and then precede to add the module. If you're not granted permission to access the httpd.conf file then contact your webhost about enabling the module.

Rewriting Dynamic URLs

Now that you've enabled the mod_rewrite module you can begin to use it. In order to keep things 101 we'll store our rewrite rules in local .htaccess files. To start declaring rewrite rules we'll turn on the rewrite engine:

Code: .htaccess

Options +FollowSymLinks
RewriteEngine on

This prepares Apache to accept rewrite rules. A RewriteRule is a simple instruction that tells mod_rewrite what to do, via regular expressions. We'll continue using our URLs from above:

Code: .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule articles/([0-9]+?)$ /index.php?id=article&article=$1

This will rewrite the URLs that were used in the previous examples. A typical RewriteRule has a specific format:

Code: .htaccess

RewriteRule (Pattern)$ (Replacement)

RewriteRule's pattern uses regular expressions. Extracted data is rephrased using the replacement string, which references to the extracted data. We can extend from our previous RewriteRule using two variables:

Code: .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule articles/([0-9]+?)/([0-9]+?)$ /index.php?id=article&article=$1&page=$2

The preceding RewriteRule rewrites a URL that contains both the article id and the page. Possibilities of RewriteRule are endless due to the vastness of regular expressions.

Summary

Apache's mod_rewrite module can do more than just rewrite dynamic URLs. It also has abilities to redirect users to a specific domain, subdomain, or folder. You've learned how to use mod_rewrite to create search engine friendly URLs.