Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active August 29, 2015 14:28
Show Gist options
  • Save khoand0000/3ee3ca5a543d630fbab6 to your computer and use it in GitHub Desktop.
Save khoand0000/3ee3ca5a543d630fbab6 to your computer and use it in GitHub Desktop.
RewriteCond   TestString  CondPattern [flags]     
  • Defines a condition under which rewriting will take place
  • backreferences: %1 to %9: groups; %0: whole matched string
  • [flags]: 'nocase|NC' (case-insensitive); 'ornext|OR'; 'novary|NV' (no vary)
RewriteRule   Pattern     Substitution [flags]    
  • Defines rules for the rewriting engine
  • if Substitution is - (dash), no replacement
  • [flags]:
    • last|L: Stop the rewriting process immediately and don't apply any more rules
    • redirect|R[=code]: Forces an external redirect, optionally with the specified HTTP status code.
    • QSA: Appends any query string from the original request URL to any query string created in the rewrite target. Example:
RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

ref: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] # no replacement (mean that keep index.php) and stop

# if request is file but not existing AND is folder but not existing, replace to /index.php
RewriteCond %{REQUEST_FILENAME} !-f     # request is file but it is not existing
RewriteCond %{REQUEST_FILENAME} !-d     # request is folder but it is not existing
RewriteRule . /index.php [L]            

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment