HTTP URL Redirection

 

ATTENTION: THIS PAGE IS Valid HTML 5 AND IS BEST VIEWED WITH HTML 5 - Please upgrade your browser or download one of the HTML 5 compatible browsers such as Mozilla Firefox, Chrome, Opera or IE 9 (March 14, 2011 or later). For more information see HTML 5 browsers.


If you find this helpful, please click the Google +1 Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.)


PDF mobile

HTTP URL Redirect

A RewriteRule is one way to redirect a request for a URL to a different page, which is called a URL redirect or URL forwarding. It is the recommended way to do a URL redirect for static web pages.

If you move a site, directory or one or more web pages you should create a URL redirect in the old location to automatically send users to the new location. To potentially preserve the page rank of those pages, it is recommended to create URL redirects on a one-to-one page-for-page basis (see Google Webmaster Central Help on "Moving Your Site"). In addition, the instructions in Google Webmaster Tools "Site configuration" - "Change of address" should be followed.

Although this also works for dynamically-generated pages it requires knowledge on how to code regular expressions.

When generating HTML with a program or server-side scripting, the language probably has an API to send the proper HTTP headers.

Regular Expression Metacharacters

Any regular expression metacharacters in the RewriteRule must be escaped with a backslash (\). These metacharacters that need to be escaped include:

  • \ - backslash
  • ^ - caret
  • $ - dollar sign
  • . - period (dot)
  • | - vertical bar (pipe)
  • ? - question mark
  • * - asterisk (star)
  • + - plus sign
  • ( and ) - parentheses
  • [ and ] - square brackets

See the URL Redirect Examples below for an example.


URL Redirect Code

Code for HTTP URL Redirect
URL redirect to a different extension
/path/.htaccess
RewriteEngine On

RewriteRule ^(.+).<#ext1#>$ /<#path/#>$1.<#ext2#> [NC,R=permanent,L]
URL redirect to a different page
/old-path/.htaccess
RewriteEngine On

RewriteRule ^<#old-page.html#>$ /<#new-path/new-page.html#> [NC,R=permanent,L]
URL redirect to a different directory
/old-path/.htaccess
RewriteEngine On

RewriteRule ^(.*)$ /<#new-path/#>$1 [R=permanent,L]

Although this is the simplest way to redirect all requests to a different directory, it blindly does the redirect to the new page. To verify that the target page exists, include a RewriteCond with -f:

/old-path/.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/<#old-path/#>(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/<#new-path/#>%1 -f
RewriteRule ^(.*)$ /<#new-path/#>$1 [R=permanent,L]

RewriteCond %{DOCUMENT_ROOT}/<#new-path/#>index.html -f
RewriteRule ^(.*)$ /<#new-path/#> [R=permanent,L]

The RewriteCond with -f for the first RewriteRule will verify that the file for the target page exists before doing the redirection there. The %1 back reference in that condition matches the regular expression group ((.*)) in the RewriteCond above it. The RewriteCond for the second RewriteRule will verify that an index.html file exists before the following RewriteRule does a redirect of any request for a document that does not exist in the new location, including a directory level request for the DirectoryIndex document. That one can be omitted if you already know that the index.html file exists in the target directory.

URL redirect to a script
/doc-root/.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} !\.<#ext#>
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{DOCUMENT_ROOT}/%1 !-d
RewriteCond %{DOCUMENT_ROOT}/%1 !-f
RewriteRule ^([^/]+)$ /index.<#ext#>?url=$1 [R=temp,L]

Here is a line-by-line explanation of that code:

  1. Line 1 eliminates the index.ext file right off the bat, to avoid redirecting requests to the script itself.
  2. In line 2, the %{REQUEST_URI}% will start with a "/" but it is not included in the subpattern, so the %1 back reference will not include the leading "/".
  3. Line 3 makes sure a directory does not exist at the requested URL.
  4. Line 4 makes sure a file does not exist at the requested URL.
  5. Line 5 does a temporary redirect (HTTP 302) to the index.ext script with the original URL path as the url parameter in the query part of the rewritten URL.
HTTP Status Code Flags in Redirect RewriteRules
R
R=temp
R=302
Found - use See Other or Temporary Redirect at user agent's discretion
R=permanent
R=301
Moved Permanently (Permanent Redirect)
R=303
See Other
R=307
Temporary Redirect

URL Redirect Examples

Examples of URL Redirects
Permanent redirect after moving pages to a different directory
/old-path/.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/old-path/(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/new-path/%1 -f
RewriteRule ^(.*)$ /new-path/$1 [R=permanent,L]

RewriteRule ^(.*)$ /new-path/ [R=permanent,L]
Permanent redirect to fix extensions after converting them from DOS 8.3 names to long names
/path/.htaccess
DirectoryIndex index.html

RewriteEngine On

RewriteRule ^(.+).htm$ /path/$1.html [NC,R=permanent,L]
Permanent redirect after moving a single page to a new location
/old-path/.htaccess
RewriteEngine On

RewriteRule ^old-page.html$ /new-path/new-page.html [NC,R=permanent,L]

Any regular expression metacharacters in the RewriteRule must be escaped with a backslash (\). See Regular Expression Metacharacters above for the list of characters that need to be escaped. Therefore, in the following example, the plus sign (+) has been escaped:

/old-path/.htaccess
RewriteEngine On

RewriteRule ^Google\+-Terminology.html$ /Blog/Entries/2011/7/23_Terminology_on_Google_Plus.html [NC,R=permanent,L]
http://www.spitballs.com/Blog/Entries/2011/7/23_Terminology_on_Google_Plus.html
URL redirect to a PHP script
/doc-root/.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} !\.php
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{DOCUMENT_ROOT}/%1 !-d
RewriteCond %{DOCUMENT_ROOT}/%1 !-f
RewriteRule ^([^/]+)$ /index.php?url=$1 [R=307,L]

Here is a line-by-line explanation of that code:

  1. Line 1 eliminates the index.php file right off the bat, to avoid redirecting requests to the script itself.
  2. In line 2, the %{REQUEST_URI}% will start with a "/" but it is not included in the subpattern, so the %1 back reference will not include the leading "/".
  3. Line 3 makes sure a directory does not exist at the requested URL.
  4. Line 4 makes sure a file does not exist at the requested URL.
  5. Line 5 does an HTTP 307 Temporary Redirect to the index.php script with the original URL path as the url parameter in the query part of the rewritten URL.

Valid HTML 5