BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

mod_rewrite: Rewrite and redirect URLs in Apache

mod_rewrite is the Apache module that rewrites and redirects URLs by rules. When you need it, how a rule works, and safe examples for common redirects.

Monthly searches · mod rewrite
1.6k
Keyword research data
mod rewrite
mod_rewrite: Rewrite and redirect URLs in Apache
Click the image to view it full size

mod_rewrite is a module of the Apache HTTP Server that changes requested URLs according to rules you write. A rule matches part of the URL with a regular expression and then either serves different content behind the same URL, sends the browser to a new URL with a redirect, or passes the request to another server. You write the rules in Apache's main configuration or, on most shared hosting, in a .htaccess file.

For SEO work it comes up in three jobs: redirecting moved pages, forcing one version of your domain (HTTPS, with or without www), and serving clean URLs from a script.

Rewrite or redirect: know which you want

The same directive does two very different things:

You want Use What the visitor and Google see
A clean URL served by a script, such as /shoes/red handled by product.php An internal rewrite (no R flag) The clean URL. Nothing changes in the address bar.
A page or site that has moved An external redirect with [R=301] The new URL, with a 301 status code

For redirects, the status code matters. Google treats a permanent redirect (301 or 308) as a signal that the new URL should be the one shown in search results. A temporary redirect (302, 303 or 307) tells Google to keep showing the old URL. A plain [R] sends a 302, so write [R=301] when a move is permanent. Google Search Central

Check first whether you need it at all

Apache's own documentation calls mod_rewrite a last resort, because rules get confusing and fragile. Simpler directives often do the job:

  • One page or folder moved: Redirect 301 /old-page.html /new-page.html (from mod_alias). RedirectMatch adds regular expressions.
  • All requests to a front controller such as index.php: FallbackResource /index.php.
  • A URL path mapped to a folder: Alias, in the server configuration.

Where you put rules matters too. If you can edit the main server configuration, put them there. Apache reads .htaccess files on every request and recompiles their regular expressions each time. mod_rewrite is the right tool when you only have .htaccess access, which is common on shared hosting and with CMSs that ship their own file.

How a rule works

A minimal rule set has three parts:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
  • RewriteEngine On switches the engine on for this context.
  • RewriteCond (optional) is a test the rule needs to pass, here the requested host name. Several conditions must all match, unless you join them with [OR].
  • RewriteRule takes a pattern, a substitution and flags. $1 inserts whatever the first bracket in the pattern matched.

In a .htaccess file, Apache strips the leading slash and the folder path before matching. In the site's root folder, /blog/post is matched as blog/post, so a pattern starting with ^/ never matches there.

The flags you'll use most:

Flag What it does
R=301 Redirect permanently (without a number, R sends a 302)
L Stop processing more rules in this pass
END Stop all rewriting. Apache prefers it for most .htaccess rules, where L can re-run the rules and loop
NC Match regardless of upper or lower case
QSA Keep the original query string and add it to the new one

Examples

These go in the .htaccess file in your site's root folder, after RewriteEngine On. Replace example.com with your domain.

Send every HTTP request to HTTPS:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

%{HTTPS} asks Apache's own SSL module, so it's "on" only when Apache itself handles the encrypted connection. If a CDN, load balancer or reverse proxy terminates HTTPS in front of your server, %{HTTPS} always reports off and this rule redirects in a loop. Apache's documentation says to test the header the proxy sets instead, usually X-Forwarded-Proto, and to trust that header only if your proxy overwrites it on every request and nothing else can reach your server directly. With access to the server configuration, Apache recommends a Redirect in the port-80 virtual host instead.

Drop the www:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

%1 is the host name the condition captured, without the www.

Serve clean URLs from a script:

RewriteRule ^products/([a-z0-9-]+)$ product.php?slug=$1 [END,QSA]

A visitor asking for /products/red-shoes stays on that URL while product.php?slug=red-shoes builds the page.

When rules go wrong

  • "Too many redirects" in the browser: a rule matches its own result. Add a condition that's false once the redirect has happened, as the HTTPS example does.
  • HTTP 500 after editing .htaccess: a typo or a directive that isn't allowed there. The server's error log says what Apache didn't accept.
  • A rule that never fires: check the leading slash in .htaccess patterns, and that the host allows rewrite rules in .htaccess files.

Test each change with curl -I https://example.com/old-page and check the status code and Location header before you move on. For deeper debugging, the server configuration can turn on rewrite logging with LogLevel alert rewrite:trace3. Apache warns that high trace levels slow the server badly, so switch it off afterwards.

SEO wiki

Related terms

More definitions from the same corner of SEO and digital marketing.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.