Difference between revisions of "Rewrite engine"

From Christoph's Personal Wiki
Jump to: navigation, search
 
(mod_rewrite)
Line 11: Line 11:
  
 
== mod_rewrite ==
 
== mod_rewrite ==
 +
 +
<div style="float:left; margin:0px 20px 20px 0px;">
 +
{| align="center" style="border: 1px solid #999; background-color:#FFFFFF"
 +
|-
 +
! colspan="4" bgcolor="#EFEFEF" | '''RewriteRule FLAGS'''
 +
|-align="center" bgcolor="#1188ee"
 +
!Flag
 +
!Description
 +
|- align="left"
 +
|<tt>R[=code]</tt> || Redirect to new URL, with optional code (see below).
 +
|--bgcolor="#eeeeee"
 +
|<tt>F</tt> || Forbidden (sends [[List of HTTP status codes|403 header]])
 +
|- align="left"
 +
|<tt>G</tt> || Gone (no longer exists)
 +
|--bgcolor="#eeeeee"
 +
|<tt>P</tt> || Proxy
 +
|- align="left"
 +
|<tt>L</tt> || Last Rule
 +
|--bgcolor="#eeeeee"
 +
|<tt>N</tt> || Next (ie, restart rules)
 +
|- align="left"
 +
|<tt>C</tt> || Chain
 +
|--bgcolor="#eeeeee"
 +
|<tt>T=mime-type</tt> || Set [[Mime Type]]
 +
|- align="left"
 +
|<tt>NS</tt> || Skip if internal sub-request
 +
|--bgcolor="#eeeeee"
 +
|<tt>NC</tt> || Case insensitive
 +
|- align="left"
 +
|<tt>QSA</tt> || Append query string
 +
|--bgcolor="#eeeeee"
 +
|<tt>NE</tt> || Do not escape output
 +
|- align="left"
 +
|<tt>PT</tt> || Pass through
 +
|--bgcolor="#eeeeee"
 +
|<tt>S=x</tt> || Skip next ''x'' rules
 +
|- align="left"
 +
|<tt>E=var:value</tt> || Set environmental variable "<tt>var</tt>" to "<tt>value</tt>".
 +
|}
 +
</div>
  
 
== User friendly / Search engine friendly URLs ==
 
== User friendly / Search engine friendly URLs ==

Revision as of 22:36, 26 July 2006

A rewrite engine is a piece of web server software used to modify URLs, for a variety of purposes. Some benefits derived from a rewrite engine are:

  • Making website URLs more user friendly
  • Making website URLs more search-engine friendly
  • Preventing undesired "inline linking"
  • Not exposing the (web address related) inner workings of a website to users

Many of these only apply to HTTP servers whose default behaviour is to map URLs to filesystem entities (i.e. files and directories); certain environments, such as many HTTP application server platforms, make this irrelevant.

The Apache HTTP server has a rewrite engine called mod_rewrite (see below), which has been described as "the Swiss Army knife of URL manipulation".

mod_rewrite

RewriteRule FLAGS
Flag Description
R[=code] Redirect to new URL, with optional code (see below).
F Forbidden (sends 403 header)
G Gone (no longer exists)
P Proxy
L Last Rule
N Next (ie, restart rules)
C Chain
T=mime-type Set Mime Type
NS Skip if internal sub-request
NC Case insensitive
QSA Append query string
NE Do not escape output
PT Pass through
S=x Skip next x rules
E=var:value Set environmental variable "var" to "value".

User friendly / Search engine friendly URLs

People use website URLs in all kinds of ways. We send them to other people by email, put them on online discussion boards, or even write them on scraps of paper. This often applies not just to website home pages, but to specific content within a website. Typically website developers want to encourage this, as it means increased traffic to their sites. As such, a well designed website should allow users to enter at any URL (not just the homepage), and the URLs throughout the site should be easy to use.

A URL is easier to use if it is short but descriptive. The URL should have some text describing the content (not just numbers), but should not be too long.

Search engines will also find it easier to index pages which follow these rules. Content which is easier to index is more likely to be included in search results.

Website URLs are often quite long and quite meaningless to humans. This is because many websites have dynamic content, meaning that HTML returned to the browser is generated on-the-fly, rather than simply being stored as a static HTML file. The URL is used not only to reference an HTML document at a fixed address, but to pass pieces of data to software running on the webserver, which then generates the HTML page dynamically. Typically this software is of the form of scripts written in a web scripting language such as Perl or PHP.

Using an URL rewrite engine, the website software can be presented with URLs in one form, while actual requests (and URLs seen by the user) are in another form. So rewrite engines allow URLs to be tidied up and made more user friendly, by configuring rewrite rules, rather than modifying the webserver software.

See also

External links