Cheatography
                https://cheatography.com
            
        
        
    
                   
                            
    
                    A mod_rewrite Cheat Sheet - a quick reference guide for mod_rewrite, with rewrite flags, regular expression syntax and sample rules.
                    
                 
                    
        
        
            
    
        
                            
        
                
        
            
                                
            
                
                                                
                                
    
    
    
            mod_rewrite RewriteRule Flags
        
                        
                                                                                    
                                                                                            C  | 
                                                                                                                        Chained with next rule  | 
                                                                                 
                                                                                            
                                                                                            CO=cookie  | 
                                                                                                                        Set specified cookie  | 
                                                                                 
                                                                                            
                                                                                            E=var:value  | 
                                                                                                                        Set environmental variable “var” to “value”  | 
                                                                                 
                                                                                            
                                                                                            F  | 
                                                                                                                        Forbidden (403 header)  | 
                                                                                 
                                                                                            
                                                                                            G  | 
                                                                                                                        Gone - no longer exists  | 
                                                                                 
                                                                                            
                                                                                            H=handler  | 
                                                                                                                        Set handler  | 
                                                                                 
                                                                                            
                                                                                            L  | 
                                                                                                                        Last - stop processing rules  | 
                                                                                 
                                                                                            
                                                                                            N  | 
                                                                                                                        Next - continue processing  | 
                                                                                 
                                                                                            
                                                                                            NC  | 
                                                                                                                        Case insensitive  | 
                                                                                 
                                                                                            
                                                                                            NE  | 
                                                                                                                        Do not escape output  | 
                                                                                 
                                                                                            
                                                                                            NS  | 
                                                                                                                        Ignore if subrequest  | 
                                                                                 
                                                                                            
                                                                                            P  | 
                                                                                                                        Proxy  | 
                                                                                 
                                                                                            
                                                                                            PT  | 
                                                                                                                        Pass through  | 
                                                                                 
                                                                                            
                                                                                            R[=code]  | 
                                                                                                                        Redirect to new URL, with optional code (see below)  | 
                                                                                 
                                                                                            
                                                                                            QSA  | 
                                                                                                                        Append query string  | 
                                                                                 
                                                                                            
                                                                                            S=x  | 
                                                                                                                        Skip next x rules  | 
                                                                                 
                                                                                            
                                                                                            T=mime-type  | 
                                                                                                                        Set mime type  | 
                                                                                 
                                                                         
                             
    
    
            mod_rewrite RewriteCond Flags
        
                        
                                                                                    
                                                                                            NC  | 
                                                                                                                        Case insensitive  | 
                                                                                 
                                                                                            
                                                                                            OR  | 
                                                                                                                        Combine with next rule using 'OR' instead of the default of 'AND'  | 
                                                                                 
                                                                         
                             
    
    
            Redirection Header Codes
        
                        
                                                                                    
                                                                                            301  | 
                                                                                                                        Moved permanently  | 
                                                                                 
                                                                                            
                                                                                            302  | 
                                                                                                                        Moved temporarily (default)  | 
                                                                                 
                                                                         
                             
    
    
            mod_rewrite Directives
        
                        
                                                                                    
                                                                                            RewriteEngine  | 
                                                                                                                        RewriteMap  | 
                                                                                 
                                                                                            
                                                                                            RewriteOptions  | 
                                                                                                                        RewriteBase  | 
                                                                                 
                                                                                            
                                                                                            RewriteLog  | 
                                                                                                                        RewriteCond  | 
                                                                                 
                                                                                            
                                                                                            RewriteLogLevel  | 
                                                                                                                        RewriteRule  | 
                                                                                 
                                                                                            
                                                                                            RewriteLock  | 
                                                                                 
                                                                         
                             
                             | 
                                                                              | 
                                                        
                                
    
    
            Regular Expressions Syntax
        
                        
                                                                                    
                                                                                            ^  | 
                                                                                                                        Start of string  | 
                                                                                 
                                                                                            
                                                                                            $  | 
                                                                                                                        End of string  | 
                                                                                 
                                                                                            
                                                                                            .  | 
                                                                                                                        Any single character  | 
                                                                                 
                                                                                            
                                                                                            (a|b)  | 
                                                                                                                        a or b  | 
                                                                                 
                                                                                            
                                                                                            (...)  | 
                                                                                                                        Group section  | 
                                                                                 
                                                                                            
                                                                                            [abc]  | 
                                                                                                                        In range (a, b or c)  | 
                                                                                 
                                                                                            
                                                                                            [^abc]  | 
                                                                                                                        Not in range  | 
                                                                                 
                                                                                            
                                                                                            \s  | 
                                                                                                                        White space  | 
                                                                                 
                                                                                            
                                                                                            a?  | 
                                                                                                                        Zero or one of a  | 
                                                                                 
                                                                                            
                                                                                            a*  | 
                                                                                                                        Zero or more of a  | 
                                                                                 
                                                                                            
                                                                                            a*?  | 
                                                                                                                        Zero or more, ungreedy  | 
                                                                                 
                                                                                            
                                                                                            a+  | 
                                                                                                                        One or more of a  | 
                                                                                 
                                                                                            
                                                                                            a+?  | 
                                                                                                                        One or more, ungreedy  | 
                                                                                 
                                                                                            
                                                                                            a{3}  | 
                                                                                                                        Exactly 3 of a  | 
                                                                                 
                                                                                            
                                                                                            a{3,}  | 
                                                                                                                        3 or more of a  | 
                                                                                 
                                                                                            
                                                                                            a{,6}  | 
                                                                                                                        Up to 6 of a  | 
                                                                                 
                                                                                            
                                                                                            a{3,6}  | 
                                                                                                                        3 to 6 of a  | 
                                                                                 
                                                                                            
                                                                                            a{3,6}?  | 
                                                                                                                        3 to 6 of a, ungreedy  | 
                                                                                 
                                                                                            
                                                                                            \  | 
                                                                                                                        Escape character  | 
                                                                                 
                                                                                            
                                                                                            [:punct:]  | 
                                                                                                                        Any punctuation symbol  | 
                                                                                 
                                                                                            
                                                                                            [:space:]  | 
                                                                                                                        Any space character  | 
                                                                                 
                                                                                            
                                                                                            [:blank:]  | 
                                                                                                                        Space or tab  | 
                                                                                 
                                                                         
                            
                             
    
    
            mod_rewrite Server Variables: HTTP Headers
        
                        
                                                                                    
                                                                                            %{HTTP_USER_AGENT}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_REFERER}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_COOKIE}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_FORWARDED}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_HOST}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_PROXY_CONNECTION}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTP_ACCEPT}  | 
                                                                                 
                                                                         
                             
    
    
            mod_rewrite Server Variables: Server Internals
        
                        
                                                                                    
                                                                                            %{DOCUMENT_ROOT}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_ADMIN}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_NAME}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_ADDR}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_PORT}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_PROTOCOL}  | 
                                                                                 
                                                                                            
                                                                                            %{SERVER_SOFTWARE}  | 
                                                                                 
                                                                         
                             
                             | 
                                                                              | 
                                                        
                                
    
    
            mod_rewrite Sample Rule: Site Moved
        
                        
                                                                                    
                                                                                            # Site moved permanently  | 
                                                                                 
                                                                                            
                                                                                            RewriteCond   %{HTTP_HOST}   ^www.domain.com$   [NC]  | 
                                                                                 
                                                                                            
                                                                                             | 
                                                                                 
                                                                         
                            Rewrites domain.com to domain2.com  
                             
    
    
            mod_rewrite Sample Rule: Temporary Page Move
        
                        
                                                                                    
                                                                                            # Page has moved temporarily  | 
                                                                                 
                                                                                            
                                                                                            RewriteRule   ^page.html$   new_page.html   [R,NC,L]  | 
                                                                                 
                                                                         
                            Rewrites domain.com/page.html to domain.com/new_page.html  
                             
    
    
            mod_rewrite Sample Rule: Nice URLs
        
                        
                                                                                    
                                                                                            # Nice URLs (no query string)  | 
                                                                                 
                                                                                            
                                                                                            RewriteRule   ^([A-Za-z0-9-]+)/?$   categories.php?name=$1 [L]  | 
                                                                                 
                                                                         
                            Rewrites domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1  
                             
    
    
            mod_rewrite Server Variables: Special
        
                        
                                                                                    
                                                                                            %{API_VERSION}  | 
                                                                                 
                                                                                            
                                                                                            %{THE_REQUEST}  | 
                                                                                 
                                                                                            
                                                                                            %{REQUEST_URI}  | 
                                                                                 
                                                                                            
                                                                                            %{REQUEST_FILENAME}  | 
                                                                                 
                                                                                            
                                                                                            %{IS_SUBREQ}  | 
                                                                                 
                                                                                            
                                                                                            %{HTTPS}  | 
                                                                                 
                                                                         
                             
    
    
            mod_rewrite Server Variables: Request
        
                        
                                                                                    
                                                                                            %{REMOTE_ADDR}  | 
                                                                                 
                                                                                            
                                                                                            %{REMOTE_HOST}  | 
                                                                                 
                                                                                            
                                                                                            %{REMOTE_PORT}  | 
                                                                                 
                                                                                            
                                                                                            %{REMOTE_USER}  | 
                                                                                 
                                                                                            
                                                                                            %{REMOTE_IDENT}  | 
                                                                                 
                                                                                            
                                                                                            %{REQUEST_METHOD}  | 
                                                                                 
                                                                                            
                                                                                            %{SCRIPT_FILENAME}  | 
                                                                                 
                                                                                            
                                                                                            %{PATH_INFO}  | 
                                                                                 
                                                                                            
                                                                                            %{QUERY_STRING}  | 
                                                                                 
                                                                                            
                                                                                            %{AUTH_TYPE}  | 
                                                                                 
                                                                         
                             
    
    
            mod_rewrite Server Variables: Time
        
                        
                                                                                    
                                                                                            %{TIME_YEAR}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_MON}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_DAY}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_HOUR}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_MIN}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_SEC}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME_WDAY}  | 
                                                                                 
                                                                                            
                                                                                            %{TIME}  | 
                                                                                 
                                                                         
                             
                             | 
                                                            
            
                            
            
            
        
        
        
        
        
            
    
        
          
        
         
Created By
https://aloneonahill.com
Metadata
Favourited By
and 56 more ...
Comments
Nice work Dave. One of the better references I've found.
I had to disable JavaScript to highlight the text due to the silly sliding tile thing.
I keep having to turn off JavaScript!!!!!!!!!!!!!!!!!!!!!
Nice. Concise and well presented. Thanks.
thanks sir . i am beginner in web developing. thanks for help . i will use your code for rewrite my query string
what is ungreedy mean ?
I have a question: ¿How can I write a rule what redirect from a URL to URL:PORT
Thanks man.. I got it. Great work done. Its a one stop shop for all rewrite formulas :)
I am trying to do a mod rewrite where it checks if the referring uri or clicked link contains a variable xyz and redirect it to another page. Or if referring uri/url has a variable ABC set, rest it to blank. Any ideas?
Good list - we also started using http:/htaccesscheatsheet.com/ (your list here has more directive/flags/server variables etc, but that website has some things you can just copy/paste in.
The link in my previous comment was a typo - it should be http://htaccesscheatsheet.com/ :)
Nice Style and well presented
Thanks !
what does exclamation mark (!) mean?
I have a Godaddy domain and I'm trying to enable cleaner urls so that a user can write www.abc.com/xyz instead of having to write www.abc.com/xyz.php but cannot get it to work. What am I doing wrong?
RewriteEngine On
Options +FollowSymLinks -MultiViews -Indexes
RewriteRule ^xyz$ xyz.php [NC,L]
This cheat sheet has been very useful to me recently, thanks for the great work !
Add a Comment
Related Cheat Sheets
More Cheat Sheets by DaveChild