Apache mod_rewrite: RewriteCond and RewriteRule

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

This Apache documentation for mod_rewrite are clear and comprehensive. This page does not intend to replace nor replicate the official documentation. However, some for of example-based tutorial may be helpful for some beginners to get them started. The examples below need to be completed and better annotated.

Let's start with the easiest example:

Let's say we want to redirect "home" to "/".

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteRule home / [redirect,last] # This example will not give the intended result!!
</IfModule>

The syntax is:
RewriteRule Pattern Substitution [flags]

where the Pattern is a perl compatible regular expression.
It means that in the example above, not only "home" will be redirected to "/", but also "home2", "home_owner" and "nice_homes"!

To get the intended result, you have to match the Pattern with the whole requested path with the proper regular expression:

  RewriteRule ^home$ / [redirect,last]

RewriteRule is fully documented here: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
The flags [redirect,last] are fully documented here: https://httpd.apache.org/docs/2.4/rewrite/flags.html

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ script.fcgi/$1 [L,QSA]
</IfModule>