Reference: mod_rewrite, URL rewriting and "pretty links" explained
Categories:
Reference: mod_rewrite, URL Rewriting, and "Pretty Links" Explained
Unlock the power of Apache's mod_rewrite module to create clean, user-friendly URLs, improve SEO, and manage website redirects effectively. This guide covers the fundamentals, common use cases, and practical examples.
In the world of web development, user experience and search engine optimization (SEO) are paramount. One critical aspect of achieving both is having clean, descriptive URLs, often referred to as "pretty links" or "friendly URLs." Instead of seeing example.com/index.php?page=about&id=123
, users and search engines prefer example.com/about-us
. This transformation is primarily handled by URL rewriting, and on Apache web servers, the mod_rewrite
module is the workhorse behind it.
Understanding mod_rewrite and .htaccess
mod_rewrite
is an Apache module that provides a powerful way to manipulate URLs. It uses a rule-based rewriting engine, based on a regular-expression parser, to rewrite requested URLs on the fly. This allows you to map complex internal URLs to simpler, more human-readable ones, or to redirect users based on various conditions.
Configuration for mod_rewrite
is typically done in two places:
- Server Configuration Files (httpd.conf): For server-wide or virtual host-specific rules. This is generally more efficient as rules are loaded once.
.htaccess
Files: Distributed configuration files placed in directories. These files allow per-directory configuration without modifying the main server configuration. While convenient for shared hosting or per-project rules, they can introduce a slight performance overhead as Apache has to parse them on every request.
.htaccess
files are convenient, for optimal performance on dedicated servers, it's recommended to place mod_rewrite
rules directly in your Apache server configuration (e.g., httpd.conf
or a virtual host file) whenever possible. This avoids the overhead of Apache scanning directories for .htaccess
files on every request.flowchart TD A[User Request: /about-us] --> B{Apache Server Receives Request} B --> C{mod_rewrite Enabled?} C -- Yes --> D{Check .htaccess/httpd.conf Rules} D -- Match Rule --> E[Rewrite URL to: /index.php?page=about] E --> F[Internal Request Processed] F --> G[Serve Content] C -- No --> H[Serve Original Request (if file exists)] H --> G D -- No Match --> H
Simplified flow of a URL rewrite request with mod_rewrite
Core Directives: RewriteRule and RewriteCond
The mod_rewrite
module primarily relies on two directives: RewriteRule
and RewriteCond
.
RewriteRule
This is the heart of the rewriting engine. It defines the actual rewriting rule. Its syntax is:
RewriteRule Pattern Substitution [Flags]
Pattern
: A regular expression that is matched against the requested URL path (after anyDocumentRoot
prefix is removed).Substitution
: The string that replaces the original URL path if thePattern
matches. This can be a file path, a URL, or a combination of both, often including backreferences ($1
,$2
, etc.) from thePattern
.Flags
: Optional modifiers that change how the rule is applied (e.g.,[L]
for Last,[R]
for Redirect,[NC]
for No Case).
RewriteCond
RewriteCond
defines conditions that must be met for the subsequent RewriteRule
to be applied. Multiple RewriteCond
directives can precede a RewriteRule
, and all must evaluate to true for the rule to fire. Its syntax is:
RewriteCond TestString Condition [Flags]
TestString
: A string to test, often a server variable (e.g.,%{REQUEST_FILENAME}
,%{HTTP_HOST}
).Condition
: A regular expression or a comparison operator that is matched against theTestString
.Flags
: Optional modifiers for the condition (e.g.,[NC]
for No Case,[OR]
to combine conditions with OR logic).
# Enable the rewrite engine
RewriteEngine On
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Rewrite /product/123 to /product.php?id=123
RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [NC,L]
Basic mod_rewrite rules in a .htaccess file
Common Use Cases and Examples
Let's explore some practical applications of mod_rewrite
.
1. Removing .php
Extensions
To make URLs cleaner, you might want to remove file extensions like .php
.
2. Forcing HTTPS
Ensuring all traffic uses HTTPS is crucial for security and SEO.
3. Trailing Slash Management
Consistency in trailing slashes (/
) helps prevent duplicate content issues.
4. Custom Error Pages
Redirect users to custom error pages for 404 Not Found, 403 Forbidden, etc.
5. Friendly URLs for Dynamic Content
This is perhaps the most common use, transforming ?id=123
parameters into path segments.
Remove .php Extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
This checks if the requested path is not a file or directory, then appends .php
.
Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This redirects all HTTP requests to their HTTPS equivalent with a 301 Permanent Redirect.
Add Trailing Slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
This adds a trailing slash to directory-like URLs that don't have one, but only if it's not an actual file.
Dynamic Friendly URLs
RewriteEngine On
RewriteRule ^article/([0-9]+)/([a-zA-Z0-9_-]+)/?$ article.php?id=$1&title=$2 [NC,L]
This rewrites /article/123/my-awesome-post/
to article.php?id=123&title=my-awesome-post
.
mod_rewrite
rules thoroughly in a development environment before deploying to production. Incorrect rules can lead to broken links, redirect loops, or server errors. Use Apache's RewriteLog
and RewriteLogLevel
directives for debugging.