Skip to main content

Rewrite / Redirect URI - Apache

Ref: Apache mod_rewrite Introduction

Ref: Apache Module mod_rewrite

Use PCRE to rewrite URL

  • !: not match
  • =: match plain string
RewriteEngine { on | off }
RewriteCond "<test string>" "[!]<RegExp condition>" [<flags>]
RewriteCond "<test string>" "[!]=<string condition>" [<flags>]
RewriteRule "<RegExp pattern>" "<substitution>" [<flags>]

Ref: RewriteCond Directive

  • By default, the query string is passed through unchanged
  • To replace query string, append ?<query> to <substitution>
  • To combin original query string, use [QSA] flag

Redirect HTTP to HTTPS

RewriteEngine on
RewriteCond "%{HTTPS}" "=off"
RewriteRule "^/?(.*)" "https://%{HTTP_HOST}/$1" [R,L]

Debug

LogLevel alert rewrite:trace3