Server Redirects
Introduction
A well designed website will have a logical file system layout, with smart folder and file names. The URLs should be short, logical, descriptive and memorable. Importantly, the address looks more permanent. In the most well designed sites, readers can guess at filenames with a high level of success.
Why Redirect
- Hide the underlying technology of the website (ASP, PHP JSP). This prevents potential hackers an idea to what type of data they should send along with the query string to perform a ‘front-door’ attack on the site.
- Prevent old URL from breaking when website technology changes.
- Easier for webmasters to link to webpage.
- Some search engines won’t index pages which they think are generated dynamically.
Syntax
Redirect [ status ] url-path url
Status
- permanent: Returns a permanent redirect status (301) indicating that the resource has moved permanently.
- temp: Returns a temporary redirect status (302). This is the default.
- seeother: Returns a “See Other” status (303) indicating that the resource has been replaced.
- gone: Returns a “Gone” status (410) indicating that the resource has been permanently removed. When this status is used the url argument should be omitted.
Other status codes can be returned by giving the numeric status code as the value of status. If the status is between 300 and 399, the url argument must be present, otherwise it must be omitted. Note that the status must be known to the Apache code
Start Rewriting
Some servers will not have » mod_rewrite enabled by default. As long as the » module is present in the installation, you can enable it simply by starting a .htaccess file with the command:
RewriteEngine on
Put this .htaccess file in your root so that rewriting is enabled throughout your site. You only need to write this line once per .htaccess file.
Rewrite Rules
- The caret, ^, signifies the start of an URL, under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret.
- The dollar sign, $, signifies the end of the string to be matched. You should add this in to stop your rules matching the first part of longer URLs.
- The period or dot before the file extension is a special character in regular expressions, and would mean something special if we didn’t escape it with the backslash, which tells Apache to treat it as a normal character.
Expressions
Using regular expressions you can have your rules matching a set of URLs at a time, and mass-redirect them to their actual pages. The parts in square brackets are called ranges.
- Digits: ([0-9])
- UPPERCASE: ([A-Z])
- lowercase: ([a-z])
- Any Case: (A-Za-z)
We have encased the regular expression part of the URL in parentheses, because we want to store whatever value was found here for later use. Once we have a value in parentheses we can use it as a back-reference. So, the first back-reference is $1, the third is $3 etc.
Modifiers
You can expand your regular expression patterns by adding some modifier characters, which allow you to match URLs with an indefinite number of characters.
- Plus sign (+) changes whatever comes directly before it, by saying ‘one or more of the preceding character or range.’
RewriteRule ^products/([0-9]+)$ /products/$1/ [R]
can be bothproducts/1orproducts/1000
- Asterisk (*) which means zero or more of the preceding character or range
- Question mark (?) means ‘zero or only one of the preceding character or range.’
The Codes
Copy the following code and save them as a .htaccess file. The .htaccess file needs to be placed in the root directory of your old website.
Redirect Old domain to New domain
Ensure that all your directories and pages of your old domain will get correctly redirected to your new domain:
Options +FollowSymLinks RewriteEngine on RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*oldwebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.preferredwebsite.net/$1 [R=301,L]
Redirect subdomains on old URL to new URL
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\olddomain\.com
RewriteRule ^(.*) http://%newdomain.com/$1 [R=301,L]
Redirect to WWW
Ensures that all requests coming in to domain.com will get redirected to www.domain.com:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Redirect All webpages in a directory to a new page
Redirect all requests to the directory to a single page:
RewriteRule ^dirName(.*)$ /fileName.php [L,R=301]
Redirect to a new page
Redirect all requests to the old page to a new page:
RewriteRule ^dirName/oldFile.html /newFile.html [L,R=301]
Redirect a dynamic URL to a page
Redirects dynamic URL's with parameters to single static file:
RewriteRule ^article.jsp?id=(.*)$ /latestnews.htm [L,R=301]
In the above example, a request to a dynamic URL such as http://www.seobook.com/article.jsp?id=8932 will be redirected to http://www.seobook.com/latestnews.htm
Redirect guessable URL to right URL
Set up a slew of ‘shortcut URLs’ that you think visitors will likely try to enter to get to pages they know exist on your site:
RewriteRule ^css(/)?$ /stylesheets/ [R]
The simple regular expression in this rule allows it to match the css URL with or without a trailing slash. The question mark means ‘zero or one of the preceding character or range’ — in other words either yourhtmlsource.com/css or yourhtmlsource.com/css/ will both be taken care of by this one rule.

