In the early months of 2001, the web is maturing. We're moving away from the 'wild west' of chaotic query strings. If your site's URLs are full of ? and & symbols, you're making a mistake. Not only are they ugly to users, but search engines like Google and AltaVista often struggle to index them deeply.
The tool of choice is mod_rewrite, famously known as the 'Swiss Army Knife of URL manipulation'. It's powerful, it's regex-based, and it can be a total nightmare if you don't understand the flags.
The Basic Rewrite Engine
To use mod_rewrite, you must enable it in your httpd.conf or your local .htaccess file. The core directive is RewriteEngine On.
# In your .htaccess file
RewriteEngine On
RewriteBase /
# Goal: Transform /products/123 to /product.php?id=123
RewriteRule ^products/([0-9]+)$ /product.php?id=$1 [L]
That single line does a lot. The ^products/([0-9]+)$ is a regular expression. The parentheses () capture the numeric ID, and $1 injects it into the destination URL. The [L] flag tells Apache to stop processing rules if this one matches.
Creating a 'Front Controller'
As we move toward more complex PHP and Perl applications, the 'Front Controller' pattern is becoming the industry standard. This involves routing all requests to a single index.php file that handles the logic.
# Route everything that isn't a real file or directory to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
The QSA (Query String Append) flag is crucial here. It ensures that if the user visits /news/latest?sort=desc, the sort=desc isn't lost during the rewrite.
Handling Domain Normalization
Professional sites in 2001 should not serve the same content on both example.com and www.example.com. This causes 'canonical URL' issues. Use mod_rewrite to force the www prefix.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The R=301 flag tells the browser (and search engines) that this is a 'Permanent Redirect'. This is essential for maintaining your search rankings.
The Regex Trap
Regex is greedy by default. If you aren't careful, you'll create rewrite loops. Always test your rules one by one. If you're on a shared host with an old version of Apache 1.3, you might find that certain variables like %{REQUEST_URI} behave slightly differently than on Apache 2.0.
Remember that mod_rewrite works in 'internal' redirects. The user sees a clean URL in their address bar, but your script sees the ugly query strings it needs to function. This is the hallmark of a professional 2001-era web architect.
Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.
Contact us to discuss your web project. See also: Web Development, E-commerce Development