|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Table of Contents
Preface (read this if nothing else)
Introduction Conventions & Definitions [used in this document] Resources & References Installation, Testing, Required & Recommended Settings per-server (httpd.conf) vs per-directory (.htaccess) Internal Rewrites vs External Redirects Internal Processing Explained (includes differences b/w .htaccess, <VirtualHost>, <Directory>, <Location>, and <Files>) Troubleshooting Example 1: /foo.html -> /foo.php (pseudo-hiding PHP) Example 2: /foo.html -> foo.html (but parsed as PHP) Example 3: /foo -> search.php?terms=foo (search on 404) Example 4: /value1/value2 -> foo.php?name1=value1&name2=value2 (known # and order) Example 5: /nameN/valueN -> /foo.php?nameN=valueN (unknown # of name=value pairs) Example 6: domain.com/foo.html -> www.domain.com/foo.html (forcing "www") Example 7: http://domain.com -> https://domain.com (forcing "https") Please PM jharnois if you have an example to add. Additional examples should not be slight variations of current examples. Last edited by jharnois : October 23rd, 2006 at 08:03 AM. Reason: Fixed link to Example 7 |
|
#2
|
||||
|
||||
|
Preface
mod_rewrite does not make /script.php?foo=bar become /script/foo/bar/ in the browser nor does it change your HTML to point to /script/foo/bar/. YOU MUST DO THIS PART YOURSELF. mod_rewrite is then used to make the pseudo-request for /script/foo/bar/ show the contents of /script.php?foo=bar
Regardless of why you're here, you're probably thinking, "do I have to read all of this?" If you're lucky enough to take an example and have it work immediately, or skilled enough to tweak it until it does, then no. Otherwise, yes. We have tried to make this as concise as possible, but mod_rewrite is very complex, very powerful, and can be used in many different contexts, each having the potential to create many nauances. The easiest way to learn is by example, examples require explanations, and those examples and explanations take space. Last edited by jharnois : May 16th, 2006 at 05:25 PM. Reason: Added purple paragraph to cover common misconception. |
|
#3
|
||||
|
||||
|
Introduction
mod_rewrite is an Apache module for URL manipulation and rewriting. It is extremely powerful and has the potential to solve any problem and be used in any situation. This power, however, is created by its complexity, and this complexity makes it difficult for new users.
This guide provides the reader with a basic understanding of how mod_rewrite works by providing and explaining examples of the most commonly requested rewrites, most of which have been taken from this forum. This guide does NOT provide comprehensive information on regular expressions (see *** below), nor does it cover Apache configuration not related to mod_rewrite, although it will link to the relevant documentation. *** The regular expressions used in rewrites are used to determine what is rewritten, not how it is rewritten. While you may not find an example that matches your needs verbatim, a quick modification to the regular expression will get you what you need. However, the example will provide you with the information on how the match is rewritten. Last edited by jharnois : August 1st, 2005 at 12:15 AM. |
|
#4
|
||||
|
||||
|
Conventions & Definitions [used in this document]
RewriteEngine On is assumed. Examples will not have the aforementioned directive, but all examples need it. It should be placed before any other Rewrite* directives.
Capatilized words in italics are directive parameters and match those in the documentation for that directive. Indented paragraphs offer a more detailed explanation, including supporting examples where applicable, for a particular point in the preceeding parent paragraph. Rewrite* directives Any of the directives listed here that are allowed in the current context. rule set Any combination of Rewrite* directives with valid arguments. Rule sets are displayed in CODE blocks. All related rule sets are displayed in a single CODE block, in which case each rule set is grouped, then separated from other groups by one or more blank lines. example(s) URLs displayed in CODE blocks in the form URL1 -> URL2 (note, if applicable), where URL1 is the original request and URL2 is the result of a single RewriteRule. If a rule set has multiple RewriteRule directives, the associated example will have multiple URL1 -> URL2s, one for each directive, and, unless otherwise stated, in the same order as those directives. Only one example is displayed per CODE block. Last edited by jharnois : January 4th, 2007 at 11:08 PM. |
|
#5
|
||||
|
||||
|
Resources & References
The following documents contain additional information, examples, explanations, etc. You should familiarize yourself with each of these documents; they are appropriately referenced throughout this document. At the very least, skim each of these to get an idea of what mod_rewrite can do and where you would go to find an example.
Apache's Official Documentation Please PM jharnois if you have a URL that you think should be added. Last edited by jharnois : July 21st, 2006 at 12:43 PM. Reason: Added resource. |
|
#6
|
||||
|
||||
|
Installation, Testing, Required & Recommended Settings
NOTE: the use of mod_rewrite requires Apache 1.3 or Apache 2.0+To test if mod_rewrite is enabled, attempt to turn the rewrite engine on using the following: NOTE: to prevent an error from affecting your entire domain, it is recommended that you create a sub-directory in your web root and run this test in that folder by using its .htaccess file. Code:
/home/foo/public_html/rewrite-test/.htaccessRewriteEngine On NOTE: When placing Rewrite* directives in .htaccess, you must have FileInfo privileges. In other words, AllowOverride FileInfo must be in httpd.conf for the directory (or one of its parents) in which .htaccess exists. NOTE: When placing Rewrite* directives in .htaccess, you must enable FollowSymLinks. In other words, Options FollowSymLinks must be in httpd.conf for the directory (or one of its parents) in which .htaccess exists, or you must add it to .htaccess. Last edited by jharnois : August 1st, 2005 at 12:18 AM. |
|
#7
|
||||
|
||||
|
per-server (httpd.conf) vs per-directory (.htaccess)
Rewrite* directives can be used in two places: per-server via the server's configuration file, httpd.conf, and per-directory via the access file, .htaccess.
NOTE: .htaccess is the default access file on all Apache servers; it can be or could have been changed using the AccessFileName directive.You should put your Rewrite* directives in httpd.conf if you have access to it, otherwise, use .htaccess (see *** below). When using httpd.conf, you can place the Rewrite* directives directly in httpd.conf, a VirtualHost, <Directory>, <Location>, or <Files> (see Interal Processing Explained for mod_rewrite related differences). NOTE: not all Rewrite* directives are allowed in all contexts. *** According to the documentation on .htaccess, it's always more efficient and secure to use httpd.conf instead of .htaccess. Using mod_rewrite in .htaccess is potentially more inefficient than other modules/directives because, according to Internal Processing » API Phases, by the time Apache gets to the point in an HTTP request where it reads .htaccess files, it's officially too late for a rewrite to occur. To solve this, mod_rewrite undoes some of Apache's work, does what it needs, then asks Apache to start its work over again.The examples provided in this document will reference .htaccess because fewer users have access to httpd.conf. httpd.conf users will need to put the provided examples in the appropriate context in httpd.conf. It is assumed, though maybe inaccurately so, that those with access to httpd.conf will have the proper understanding to place the Rewrite* directives in the correct place and make the necessary adjustments (see Internal Processing Explained for mod_rewrite related differences). In the following two examples, the rewrite engine is turned on. The first demonstrates doing so in a per-directory context using .htaccess while the second demonstrates doing so in a per-server context using a <Directory> located in httpd.conf. The result is the same. Code:
/home/foo/public_html/.htaccess
RewriteEngine On Code:
httpd.conf
<Directory /usr/foo/public_html> RewriteEngine On </Directory> Last edited by jharnois : August 1st, 2005 at 12:39 AM. |
|
#8
|
||||
|
||||
|
Internal Rewrites vs External Redirects
An internal rewrite shows the contents of one file while displaying the location or pseudo-location of another file in the browser's address bar. These are common for optimizing URLs for search engines because they can effectively hide a query string. They cannot be used to show the contents of a file on a different server or domain.
In the following example, all HTML requests will show the results of the PHP file with the same name. Code:
RewriteRule ^(.+)\.html$ $1.php Code:
domain.com/foo.html -> domain.com/foo.php An external redirect will change the browser's address bar to the resulting URL. You should only use an external redirect when the target file is located on a different server or domain, or when you need to provide a server status code. mod_rewrite's [R] flag performs both of these tasks. In the following example, all HTML requests on this domain are redirected to the HTML file with the same name on anotherdomain. Code:
RewriteRule ^(.+\.html)$ http://anotherdomain.com/$1 [R] Code:
domain.com/foo.html -> anotherdomain.com/foo.html NOTE: External redirects can also be obtained using Apache's mod_alias through the Redirect and RedirectMatch directive. These do not require mod_rewrite to be enabled. If you find yourself using a RewriteRule that results in an external redirect, and there is no corresponding RewriteCond, Redirect or RedirectMatch is the recommended alternative. |
|
#9
|
||||
|
||||
|
Internal Processing Explained
Because mod_rewrite works internally and often invisibly, it is extremely important to understand what is going on when Apache encounters a rule set. This understanding will help you determine the proper rules and conditions and troubleshoot any problems. Processing Order Please read Internal Processing » Ruleset Processing before continuing. Then, read it again, slower this time, making an effort to understand exactly what each sentence is telling you in relation to any examples provided in the documentation. I realize this sounds elementary, but it is essential to understanding mod_rewrite. In the following rule set, we rewrite all GIF requests to the corresponding JPG file with the same name. In the following rule set, each request is checked to see if it ends with ".gif". If it does not, the rule is skipped. If it does, the request is rewritten to show the same file, but with a ".jpg" extension instead of a ".gif" extension. Code:
RewriteRule ^(.+)\.gif$ $1.jpg Code:
domain.com/foo.gif -> domain.com/foo.jpg Code:
domain.com/bar/baz.gif -> domain.com/bar/baz.jpg Code:
RewriteCond %{REQUEST_URI} ^/png/
RewriteRule ^(.+)\.jpg$ $1.png
Code:
domain.com/foo.jpg -> domain.com/foo.jpg Code:
domain.com/png/foo.jpg -> domain.com/png/foo.png NOTE: There is no need to prepend "/png/"; mod_rewrite will do this for you.Combining the previous two rule sets will have these results: a request for a GIF file outside the "png" directory will result in the corresponding JPG file with the same name being shown, but a requst for a GIF file inside the "png" directory will result in the corresponding PNG file with the same name being shown. Why? The first RewriteRule rewrites the GIF file to a JPG file, then the second RewriteRule rewrites the JPG file to a PNG file, but only if that JPG file is in the png diretory, because only then will the RewriteCond be true. Remember, the first RewriteRule applies to all GIF files, including those in the "png" directory. |