Variable Visions

.htaccess and MOD_REWRITE Rules, Conditions, and confusion

Published Thu. Aug. 02, 2012

Apache's MOD_REWRITE allows regular expression rules to be used to redirect ugly query string URLs into SEO/SEF friendly URLs.

The .htaccess configuration file (or hypertext access file) located in the root directory of your web site has some powerful options for redirecting URLs, as well as many other server configuration options and settings. While the httpd.conf file should be considered the main configuration file for an Apache web server, those of us who use a shared hosting provided do not have access to this file. But we can use the .htaccess file to achieve many of the same tasks, using the same conditions and rules.
If you are not already using the .htaccess file for custom server configuration, you've probably noticed the file in the same directory as your index.php file. (Although you can use multiple .htaccess files in multiple directories - these are directory-level configuration files and each controls files in the same directory). Sometimes you'll see an htaccess.txt file which needs to be renamed to .htaccess to work properly on an Apache web server. IIS web servers have their own counterpart, but I'm only dealing with .htaccess on Apache.
Some of the more common lines of code you can use are as follows:
ErrorDocument 404 /404.php (This allows the use of a custom 404 error page - the /404.php is the location of the file relative to root)?IndexIgnore * (This allows the index view to be hidden from the browser)
The real power is the redirect conditions and rules.
Make sure you start your file with: ?Options +FollowSymlinks ?RewriteEngine on
In the case of this web site, variablevisions.com, I wanted one RewriteRule to redirect all the article query string URLs (created using the GET method variable used for 'title' in the SQL query).
This is important to keep the URLs SEO and SEF. This concept is particularly important when your query string URLs use unique numeric identifiers from fields on your database.
Even thought the redirect example below both contain the keywords, the prior using the index.php?= which some search engines are known to truncate the characters preceding.
http://www.variablevisions.com/index.php?title=htaccess-mod-rewrite-rules-conditions-and-confusion (is the actual URL, but you can use the later in your links and site-map)?http://www.variablevisions.com/articles/htaccess-mod-rewrite-rules-conditions-and-confusion
I learned the hard way how to use variables in a RewriteRule. Remember these regular expressions start with ^ and end with $.
You can simply have these...
RewriteRule ^category-name-01$ ?id=1?RewriteRule ^category-name-02$ ?id=2 ?RewriteRule ^category-name-03$ ?id=3
So that...
http://www.yoursite.com/?id=1
would redirect to...
http://www.yoursite.com/category-name-01
But then you would need a new rewrite rule for every category. I set up this site using this method for over 100 tags. A few find and replaces later and I had the list of RewriteRules, but I dreaded doing this for every single article post!
I then discovered the magic...if you equal your GET variable to $1 ($2 for the next variable, etc.) and its counterpart is the (.*)
This means that I can use the SEF URLs for links but the actual page that is displayed is the index.php script with the GET variable specified.
http://www.yoursite.com/index.php?title=this-great-article
will redirect to:
http://www.yoursite.com/articles/this-great-article
Great for your site-map and SEF/SEO practices. And the best part, any new article posted will be able to take advantage of this one RewriteRule.
THIS IS THE MAGIC:
RewriteRule ^articles/(.*) /index.php?title=$1
The above rule is two parts: ^articles/(.*) and?/index.php?title-$1
The first part is what you would like your URL to be displayed as. The friendly part if you will.?The word "articles" is description, yet arbitrary. You can use any word here you like for this "virtual" directory. The word DOES NOT need to match any GET variable or any field on your database, it can be whatever you see fit. The /(.*) uses the wildcard character and is an "anything goes" condition. This means, basically, that /(.*) in the first part of the rule and corresponds to the index.php?title=$1 and will get its information from the $1 variable.
The second part of the rule is the original URL (in this case using the GET variable "title" (in the index.php script) to create the query string) but the $1 is the variable and will change (and be inserted after articles/ in the first part) for each new article inserted into the database. This allows us the use of only ONE redirect rule (and no condition) for ALL articles on the site!
This is the first step in developing an SEO/SEF web site.
Check back in later for more articles on Apache server configuration and the use of .htaccess and the rewrite module.

Keywords:htaccess, mod-rewrite, apache, SEO, SEF, URL redirects