Apache Rewrite Rules Guide
A strong understanding of rewriting URL is very important for almost any web application. We will discussed Basics of Apache Rewrite Rules in this article. Beside Apache web server-spesific concepts, many other modules do exist for other web servers. However, we will focussed on Apache Rewriting Rules in this article.
Rewrite rules are one of the important pieces of the PHP and MySQL puzzle and mostly found in every major web application that uses PHP. Rewrite rules serve two main purposes:
- Creating clean, seo friendly and readable URLs
- Hiding the underlying functionality of PHP so it will improving security of site internals
Rewrite rules are most often used for masking complex URLs to improve SEO (Search Engine Optimization) and making URL easier to read. Let’s see example below
- http://technotif.com/post.php?id=025
- http://technotif.com/apache-rewrite-rules-guide
The second url is easier to read and improving search engine optimization because it contains keyword that a search engine crawler can pick up. Rewriting rules also hiding post.php?id=025 which has benefit of not exposing PHP to the client. So it’s improving website’s security. To allow reader accessing nodes through friendly name, Use these following scripts in .htaccess file.
RewriteRule Directive
RewriteRule Directive defines rule for the rewriting engine. Syntax of RewriteRule is
1 RewriteRule Pattern Substitution [flags]
Here is an example of RewriteRule directive using .htaccess
123 RewriteEngine OnRewriteRule ^([0-9]{4})/([0-9]{0,2})/([^/]+)/$ /post.php?y=$1&m=$2&t=$4 [S]RewriteRule ^view\.php - [F]
This is an example script of rewriting rules. Each line is called a directive. First line is turns on Rewrite Engine. Second line is matching URL pattern using RegEx. The third line detects if the url is post script and forces redirect it to a 403 (Forbidden Response). [S] and [F] are .htaccess flags.
List .htaccess Flags on Apache Rewrite Rules
There are 15 flags in total. Flags can be defined at the end of a rule to manipulate Apache, mostly are only read if the rule matches. [S] and [F] flags already used in previous example.
Flag | ShortForm | Usage |
---|---|---|
Redirect | R | Redirect Flag is used to redirect an URL. The status code is 302 (Found). You can change status code to 301 (Moved Permanently) by using [R=301] Flag |
Forbidden | F | Immediately returns a 403 (Forbidden) status code and stop processing |
Gone | G | Returns a 410 (Gone) response status code. Used to indicate the file is no longer exist |
Proxy | P | Indicates that the request is a proxy request. Request will be handled by mod_proxy |
Last | L | Causes mod_rewrite to stop processing rule set. |
Next | N | The Next flag causes the ruleset to start over again from the top, using the result of the rule set so far as a starting point |
Chain | C | The Chain flag indicates that the RewriteRule is chained to the next rule |
NoSubreq | NS | The NS flag used to skips the rule if the current request is an internal sub-request and not a direct HTTP request by the client. |
NoCase | NC | The NC flag used to Indicates that the condition is case-insensitive. |
QSAppend | QSA | The QSA flag used to Appends the query string to the end of the new URL. With the QSA flag, a request for /posts/123?one=two will be mapped to /post.php?post=123&one=two. Without the QSA flag, that same request will be mapped to /post.php?post=123 – the existing query string will be discarded. |
NoEscape | NE | The noescape flag used when the replacement URL is already escaped. Without this flag characters such as % and $ are replaced with their hex code equivalents. |
Passthrough | PT | The passthrough (PT) flag used to Passes the new URL to the next Apache module. This allows multiple modules that do URL translation or matching to work together. For example: mod_rewrite and mod_alias. |
Skip | S | The S flag used to Skips the next rule if the current rule matches. Multiple rules can also be skipped by specifying a number. For example: [S=2]. |
Env | E | The env flag used to Sets an environmental variable, which can then be read by PHP. |
Type | T | The T flag used to Sets the MIME type with which the resulting response will be sent. |
It’s possible to use multiple flags for a single directive rule ( For example, [L, QSA] ).
List of Conditional Rules
Using the RewriteCond directive can made Rewrite rules be conditional. instead of using pattern in RewriteRule syntax you can use Conditional Rule. Here is a list of conditional rules that can be used on .htaccess
- HTTP Headers
- HTTP_HOST
- HTTP_REFERER
- HTTP_COOKIE
- HTTP_FORWARDED
- HTTP_USER_AGENT
- HTTP_PROXY_CONNECTION
- HTTP_ACCEPT
- CONNECTION AND REQUEST
- REMOTE_IDENT
- REMOTE_HOST
- REMOTE_USER
- REMOTE_ADDR
- REQUEST_METHOD
- SCRIPT_FILENAME
- PATH_INFO
- QUERY_STRING
- AUTH_TYPE
- INTERNAL
- DOCUMENT_ROOT
- SERVER_ADMIN
- SERVER_ADDR
- SERVER_NAME
- SERVER_PORT
- SERVER_PROTOCOL
- SERVER_SOFTWARE
- API_VERSION
- THE_REQUEST
- REQUEST_URI
- REQUEST_FILENAME
- IS_SUBREQ
- TIME AND DATE
- TIME_YEAR
- TIME_MON
- TIME_WDAY
- TIME_HOUR
- TIME_MIN
- TIME_SEC
- TIME_DAY
- TIME