Why Use 301 Redirects
During website development, there are many situations that require URL redirection: changes in directory structure, renamed pages, changed file extensions, domain name changes, etc. Without redirection, users’ bookmarks and old URLs stored in search engine databases will only lead visitors to a 404 error page, resulting in lost traffic. Moreover, all the previous accumulation for that page (such as PageRank) will be wasted.
A 301 redirect not only automatically redirects the page for users, but may also pass PageRank value to the new URL for search engines.
Detailed Guide to Nginx Rewrite Rules
http://www.jefflei.com/post/1015.html
The rewrite Command
Nginx’s rewrite is equivalent to Apache’s RewriteRule (in most cases, you can take existing Apache rewrite rules and wrap them in quotes to use directly). It can be used in server, location, and IF conditional blocks. The command format is as follows:
rewrite regex replacement flag
The flag can be one of the following formats:
last – Basically, use this flag in most cases.
break – Stops Rewrite processing and no further matching occurs.
redirect – Returns a temporary redirect with HTTP status 302.
permanent – Returns a permanent redirect with HTTP status 301.
For example, the following configuration tells Nginx to redirect files from one directory to another; $2 corresponds to the string matched in the second set of parentheses (.*):
location /download/ {
rewrite ^(/download/.*)/m/(.*)/..*$ $1/nginx-rewrite/$2.gz break;
}
Nginx Redirect IF Conditionals
In both server and location contexts, you can use Nginx IF conditionals. The conditions can be of the following types:
Regular Expressions
Like:
Matching Checks
~ is for case-sensitive matching; !~ is for case-sensitive non-matching.
~* is for case-insensitive matching; !~* is for case-insensitive non-matching.
For example, the following configuration redirects users to the /nginx-ie directory when they are using Internet Explorer:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
File and Directory Checks
-f and !-f check if a file exists.
-d and !-d check if a directory exists.
-e and !-e check if a file or directory exists.
-x and !-x check if a file is executable.
For example, the following sets up Nginx to redirect when a file or directory does not exist:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1/;
}
return
Returns an HTTP status code. For example, to set up Nginx hotlink protection:
location ~* /.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked http://www.123.com/ http://www.leizhenfang.com/;
if ($invalid_referer) {
return 404;
}
}
set
Sets an Nginx variable.
301 Redirect Methods
To implement a 301 redirect, consolidating www.123.com and 123.com, and merging the previous domain as well, there are two implementation methods. The first method checks the Nginx core variable host (http_host in older versions):server {
server_name www.123.com 123.com ;
if ($host != 'www.123.com' ) {
rewrite ^/(.*)$ http://www.123.com/$1 permanent;
}
...
}
The second method:server {
server_name jefflei.com;
rewrite ^/(.*) http://www.jefflei.com/$1 permanent;
}
Tested the first method and it’s OK. In both of these methods, permanent is the key. For a detailed explanation, see the Nginx redirect rules section.
last – Basically, use this flag in most cases.
break – Stops Rewrite processing and no further matching occurs.
redirect – Returns a temporary redirect with HTTP status 302.
permanent – Returns a permanent redirect with HTTP status 301.
Alright, now you can check the result. Here you can view the returned HTTP header information.
The second method was not successfully tested…
Testing If the Redirect Is Successful
Enter the command~
/usr/local/nginx/sbin/nginx -t
This should prompt:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
Test successful~ Restart Nginx~ Enter the command~
/usr/local/nginx/sbin/nginx -s reload
After restarting, test it~ to see if the setup was successful! Enter the command~
curl -I 123.com
It will output:
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.65
Date: Tue, 03 Aug 2010 01:12:37 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.123.com