How does a mod rewrite work?

mod_rewrite is an Apache module that allows you to rewrite URLs on your website using regular expressions. Here’s a brief explanation of how it works:

  1. User requests a URL: When a user requests a URL from your website, the request is sent to the web server.
  2. mod_rewrite checks the URL: If mod_rewrite is enabled on your web server and you have a rewrite rule in place, mod_rewrite checks the requested URL against the rule.
  3. Rewrite rule matches: If the requested URL matches the rewrite rule, mod_rewrite rewrites the URL and sends the request back to the web server.
  4. New URL is processed: The web server processes the new URL and returns the requested content to the user.

For example, let’s say you have a website with the following URL: You want to make this URL more user-friendly by removing the “.php” extension and replacing the query string with a more descriptive string. Here’s how you could use mod_rewrite to do this:

In this example, the rewrite rule matches any URL that starts with “/page/” followed by a number, followed by an optional trailing slash. The “([0-9]+)” captures the number from the URL and passes it to the “id” parameter in the query string. The “[L]” flag tells mod_rewrite to stop processing any further rules if this rule matches.

So, when a user requests the URL, mod_rewrite rewrites the URL to, which the web server processes and returns the requested content to the user. The user sees the user-friendly URL in their browser, but the server processes the original URL behind the scenes.

Leave a Reply