Home Apache/HTTPD Helper
Post
Cancel

Apache/HTTPD Helper

Reminder of RewriteRule flags :

RewriteRule FlagsWhat it does  
LLast. Stop processing RewriteRules once this one matches. Order counts!  
CChain. Continue processing the next RewriteRule. If this rule doesn’t match, then the next rule won’t be executed. More on thislater. 
ESet environmental variable. Apache has various environmental variables that can affect web-server behavior.  
FForbidden. Returns a 403-Forbidden error if this rule matches.  
GGone. Returns a 410-Gone error if this rule matches.  
HHandler. Forces the request to be handled as if it were the specified MIME-type.  
NNext. Forces the rule to start over again and re-match. BE CAREFUL! Loops can result.  
NCNo case. Allows [jpg] to match both jpg and JPG.  
NENo escape. Prevents the rewriting of special characters (. ? # & etc) into their hex-code equivalents.  
NSNo subrequests. If you’re using server-side-includes, this will prevent matches to the included files.  
PProxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-serverfetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad. 
PTPass Through. Take into account Alias statements in RewriteRule matching.  
QSAQSAppend. When the original string contains a query (http://example.com/thing?aspfoo) append the original query string to therewritten string. Normally it would be discarded. Important for dynamic content.
RRedirect. Provide an HTTP redirect to the specified URL. Can also provide exact redirect code [R303]. Very similar toRedirectMatch, which is faster and should be used when possible.
SSkip. Skip this rule.  
TType. Specify the mime-type of the returned content. Very similar to the AddType directive.  

Example

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

Example 1 - Redirect example.com to www.example.com:

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Example 2 - Redirect www.example.com to example.com:

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

Links:

This post is licensed under CC BY 4.0 by the author.