mod_rewrite is a powerful web server module that allows you to modify incoming URL requests on the fly. It is primarily used to create “clean” URLs, redirect old pages to new ones, and improve website security by masking the underlying file structure.
Table of Contents
Context
When a visitor enters a URL, the server usually looks for a specific file (e.g., sklep.php). With mod_rewrite, the server can “intercept” that request and show content from a different path without the user-or search engines-seeing the complex original address. This process is managed via the .htaccess file in your website’s directory.
Why use mod_rewrite?
Standard dynamic URLs often look like this: http://www.mycompany.com/shop.php?category=hats&id=123
By applying rewrite rules, you can transform them into: http://www.mycompany.com/products/hats/123/
Key benefits include:
- Search Engine Optimization (SEO): Static-looking links are more easily indexed by search engines.
- User Experience: Clean links are easier to remember, read, and share.
- Security: Masking script names and variable parameters hides the technical structure of your site from potential attackers.
- Stability: If you change your site’s backend technology, you can keep the same URL structure, preventing broken links.
How it works: Basic Rules
To use mod_rewrite, you must include the directive RewriteEngine on at the top of your .htaccess file.
1. Simple Internal Redirect
If you renamed page.html to new_page.html, you can ensure the old link still works:
Apache
RewriteEngine on
RewriteRule ^page\.html$ new_page.html
- The
^marks the start of the path. - The
$marks the end of the path. - The
\before the dot tells the server to treat the dot as a literal character, not a special command.
2. Forcing a Visible Redirect [R]
If you want the user’s browser address bar to actually update to the new URL, add the [R] flag:
Apache
RewriteRule ^page\.html$ new_page.html [R] Advanced Rewriting (Regular Expressions)
You can use “wildcards” to handle multiple addresses at once. This is common for product catalogs.
Example: Capturing IDs
Apache
RewriteRule ^products/([0-9]+)/$ /info.php?ID=$1
([0-9]+): Matches any sequence of numbers.$1: Takes the number found in the first set of parentheses and plugs it into the real URL.- Result: A request for
/products/123/internally loads/info.php?ID=123.
Distinction: Internal Rewrite vs. External Redirect
- Internal Rewrite: The server shows the new content, but the user’s browser address stays the same. It is a “transparent” change.
- External Redirect
[R]: The server tells the browser to go to a new address. The URL in the address bar changes. This is better for moving content permanently.
Practical Implications
- Order Matters: Rules in the
.htaccessfile are processed from top to bottom. Place specific rules (like individual page moves) before general rules (like category-wide rewrites). - Syntax Sensitivity: Small typos in an
.htaccessfile can result in a 500 Internal Server Error. Always keep a backup of your working file before making changes. - Trailing Slashes: Be consistent with your use of slashes. A rule for
products/12/will not catchproducts/12unless you specifically add a rule to handle both.
Summary
The mod_rewrite module is an essential tool for maintaining professional, SEO-friendly, and secure URL structures. By mastering a few basic rules and regular expressions, you can ensure your website’s navigation remains logical and permanent regardless of technical changes.