Why use 301 redirection?
There are many situations where webpage redirection is required during website construction, such as webpage directory structure change, webpage rename, webpage extension change, and website domain name change. If no redirection is performed, the old address in the user's favorites and search engine database can only give the visitor a 404 error page, and the access traffic is lost. In addition, all the previous page accumulation (such as the PR value) is in vain.
301 redirection not only enables automatic page Jump, but also allows the search engine to pass the prvalue.
Nginx redirection rules
Http://www.jefflei.com/post/1015.html
Rewrite command
Nginx rewrite is equivalent to apache rewriterule (in most cases, the original apache rewrite rules can be directly used with quotation marks). It can be used in server, location, and IF condition judgment blocks, the command format is as follows:
Replace the target flag with the rewrite Regular Expression
The flag can be in the following formats:
Last-this Flag is basically used.
Break-Abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection
For example, the following section sets nginx to redirect the files under a directory to another directory. $2 corresponds to the corresponding string in the second bracket:
Location/download /{
Rewrite ^ (/download/. *)/m/(. *) \... * $1/nginx-rewrite/restart 2.gz break;
}
IF condition judgment for nginx redirection
The IF condition of nginx can be used to determine the server and location. The conditions can be as follows:
Regular Expression
For example:
Matching and judgment
~ It is case-sensitive ;!~ Case-insensitive
~ * Case-insensitive match ;!~ Case-insensitive
For example, set nginx to redirect to the/nginx-ie directory when you use ie:
If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
File and directory judgment
-F and! -F indicates whether a file exists.
-D and! -D: determines whether a directory exists.
-E and! -E: Determine whether a file or directory exists.
-X and! -X determines whether the file is executable.
For example, set nginx to redirect when the file and directory do not exist:
If (! -E $ request_filename ){
Proxy_pass http: // 127.0.0.1 /;
}
Return
Return the http Code, for example, setting nginx anti-leech:
Location ~ * \. (Gif | jpg | png | swf | flv) $ {
Valid_referers none blocked http://www.jefflei.com/http://www.leizhenfang.com /;
If ($ invalid_referer ){
Return 404;
}
}
Set
Set nginx Variables
301 redirection Method
301 redirection is performed, www .jefflei.com and jefflei.com are merged, and the previous domain names are also merged. there are two implementation methods. The first method is to determine the nginx core variable host (the old version is http_host ):
server {
server_name www.jefflei.com jefflei.com ;
if ($host != 'www.jefflei.com' ) {
rewrite ^/(.*)$ http://www.jefflei.com/$1 permanent;
}
...
}
Method 2:
server {
server_name jefflei.com;
rewrite ^/(.*) http://www.jefflei.com/$1 permanent;
}
Tested
The first method is OK. permanent is the key of the two methods. For details, see the nginx redirection rules.
Last-this Flag is basically used.
Break-Abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection
Now we can check the results. Here we can view the returned HTTP header information:
Http://www.seoconsultants.com/tools/headers.asp
The second method is not successfully tested...
Test whether the targeting is successful
Http://qinfy.net/301-redirect-for-nginx/
Input command ~
/Usr/local/nginx/sbin/nginx-t
Tip:
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 ~ Input command ~
/Usr/local/nginx/sbin/nginx-s reload
Test it after restart ~ Whether the setting is successful! Input command ~
Curl-I imcat. tk
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://qinfy.net/
Nginx rewrite pseudo-static configuration parameters)
Http://hi.baidu.com/hx10/blog/item/942a0ad784f3ffd0a144df94.html
Nginx rewrite pseudo-static configuration parameters and Use Cases with regular expression instructions
Regular Expression matching, where:
*~ Case-sensitive matching
*~ * Case-insensitive match
*!~ And !~ * Case-insensitive and case-insensitive
File and directory match, where:
*-F and! -F is used to determine whether a file exists.
*-D and! -D is used to determine whether a directory exists.
*-E and! -E is used to determine whether a file or directory exists.
*-X and! -X is used to determine whether a file is executable.
Flag labels include:
* Last is equivalent to the [L] Mark in Apache, indicating that rewrite is completed.
* The break terminates the match and does not match the subsequent rules.
* If redirect returns the 302 temporary redirection address bar, the redirected address is displayed.
* If permanent returns 301, the address bar of the permanent redirection will display the address after the jump.
Some available global variables can be used for condition determination (to be supplemented)
$ Args
$ Content_length
$ Content_type
$ Document_root
$ Document_uri
$ Host
$ Http_user_agent
$ Http_cookie
$ Limit_rate
$ Request_body_file
$ Request_method
$ Remote_addr
$ Remote_port
$ Remote_user
$ Request_filename
$ Request_uri
$ Query_string
$ Scheme
$ Server_protocol
$ Server_addr
$ Server_name
$ Server_port
$ Uri
Example of combining QeePHP
If (! -D $ request_filename ){
Rewrite ^/([a-z-A-Z] +)/([a-z-A-Z] + )/? (. *) $/Index. php? Namespace = user & controller = $1 & action = $2 & $3 last;
Rewrite ^/([a-z-A-Z] + )/? $/Index. php? Namespace = user & controller = $1 last;
Break;
Convert multiple directories to parameters
Abc.domian.com/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2
If ($ host ~ * (. *) \. Domain \. com ){
Set $ sub_name $1;
Rewrite ^/sort \/(\ d + )\/? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last;
}
Directory swap
/123456/xxxx->/xxxx? Id = 123456
Rewrite ^/(\ d +)/(. +) // $2? Id = $1 last;
For example, set nginx to redirect to the/nginx-ie directory when you use ie:
If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
Automatically add "/" to the directory
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
Disable htaccess
Location ~ /\. Ht {
Deny all;
}
Prohibit multiple directories
Location ~ ^/(Cron | templates )/{
Deny all;
Break;
}
Prohibit files starting with/data
You can disable/data/multi-level directory. log.txt and other requests;
Location ~ ^/Data {
Deny all;
}
Disable a single directory
Unable to stop. log.txt requests
Location/searchword/cron /{
Deny all;
}
Prohibit a single file
Location ~ /Data/SQL/data. SQL {
Deny all;
}
Set the expiration time for favicon.icoand robots.txt;
Here, favicon. ico is set to 99 days, And robots.txt is set to 7 days, and no 404 error logs are recorded.
Location ~ (Favicon. ico ){
Log_not_found off;
Expires 99d;
Break;
}
Location ~ (Robots.txt ){
Log_not_found off;
Expires 7d;
Break;
}
Set the expiration time of a file. The value is 600 seconds and no access logs are recorded.
Location ^ ~ /Html/scripts/loadhead_1.js {
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 600;
Break;
}
File anti-leeching and set expiration time
Here, return 412 is a custom http status code. The default value is 403, which helps you find the correct request for leeching.
"Rewrite ^/http://leech.c1gstudio.com/leech.gif?#display an anti-leech Image
"Access_log off;" does not record access logs, reducing pressure
"Expires 3d" browser cache for all files for 3 days
Location ~ * ^. + \. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
Valid_referers none blocked * .c1gstudio.com * .c1gstudio.net localhost 208.97.167.194;
If ($ invalid_referer ){
Rewrite ^/http://leech.c1gstudio.com/leech.gif;
Return 412;
Break;
}
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 3d;
Break;
}
Only use a fixed ip address to access the website and add a password.
Root/opt/htdocs/www;
Allow 208.97.167.194;
Allow 222.33.1.2;
Allow 231.152.49.4;
Deny all;
Auth_basic "C1G_ADMIN ";
Auth_basic_user_file htpasswd;
Convert files under multi-level directories into one file to improve seo Performance
/Job-123-456-789.html to/job/123/456/789.html
Rewrite ^/job-([0-9] +)-([0-9] +)-([0-9] + )\. html $/job/$1/$2/jobshow_#3.html last;
Point a folder in the root directory to a Level 2 Directory
For example,/shanghaijob/points to/area/shanghai/
If you change last to permanent, the address bar of the browser is/location/shanghai/
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
The problem in the above example is that the access/shanghai will not match
Rewrite ^/([0-9a-z] +) job $/area/$1/last;
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
In this way,/shanghai can also be accessed, but the relative link on the page cannot be used,
For example, if the actual address of./list_1.html is/area/shanghia/list_1.html, it will become/list_1.html and cannot be accessed.
I cannot add automatic jump.
(-D $ request_filename) it has a condition that it must be a real directory, and my rewrite is not, so it has no effect
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
After knowing the reason, let me jump to it manually.
Rewrite ^/([0-9a-z] +) job $/$ 1job/permanent;
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
Redirection when the file and directory do not exist:
If (! -E $ request_filename ){
Proxy_pass http: // 127.0.0.1 /;
}
Domain jump
Server
{
Listen 80;
Server_name jump.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/http://www.c1gstudio.com /;
Access_log off;
}
Multi-domain redirection
Server_name http://www.c1gstudio.com/http://www.c1gstudio.net /;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs;
If ($ host ~ "C1gstudio \. net "){
Rewrite ^ (. *) http://www.c1gstudio.com $1/permanent;
}
Third-level domain jump
If ($ http_host ~ * "^ (. *) \. I \. c1gstudio \. com $ "){
Rewrite ^ (. *) http://top.yingjiesheng.com $1 /;
Break;
}
Domain Name mirror
Server
{
Listen 80;
Server_name pai.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/(. *) http://www.c1gstudio.com/#1 last;
Access_log off;
}
A subdirectory for mirroring
Location ^ ~ /Zhaopinhui {
Rewrite ^. + http://zph.c1gstudio.com/last;
Break;
}
Discuz ucenter home (uchome) rewrite
Rewrite ^/(space | network)-(. +) \. html $/$ 1.php? Rewrite = $2 last;
Rewrite ^/(space | network) \. html $/$ 1.php last;
Rewrite ^/([0-9] +) $/space. php? Uid = $1 last;
Discuz 7 rewrite
Rewrite ^ (. *)/archiver/(fid | tid)-[\ w \-] + \. html) $1/archiver/index. php? $2 last;
Rewrite ^ (. *)/forum-([0-9] +)-([0-9] +) \. html $1/forumdisplay. php? Fid = $2 & page = $3 last;
Rewrite ^ (. *)/thread-([0-9] +)-([0-9] +)-([0-9] + )\. html $1/viewthread. php? Tid = $2 & extra = page \ % 3D $4 & page = $3 last;
Rewrite ^ (. *)/profile-(username | uid)-(. +) \. html $1/viewpro. php? $2 = $3 last;
Rewrite ^ (. *)/space-(username | uid)-(. +) \. html $1/space. php? $2 = $3 last;
Rewrite ^ (. *)/tag-(. +) \. html $1/tag. php? Name = $2 last;
Configure a domain name for a forum in discuz
Server_name bbs.c1gstudio.com news.c1gstudio.com;
Location = /{
If ($ http_host ~ News \ .c1gstudio.com $ ){
Rewrite ^. + http://news.c1gstudio.com/forum-831-1.html last;
Break;
}
}
Optimization of discuz ucenter profile rewrite
Location ^ ~ /Ucenter {
Location ~ . * \. Php? $
{
# Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}
Location/ucenter/data/avatar {
Log_not_found off;
Access_log off;
Location ~ /(. *) _ Big \. jpg $ {
Error_page 404/ucenter/images/noavatar_big.gif;
}
Location ~ /(. *) _ Middle \. jpg $ {
Error_page 404/ucenter/images/noavatar_middle.gif;
}
Location ~ /(. *) _ Small \. jpg $ {
Error_page 404/ucenter/images/noavatar_small.gif;
}
Expires 300;
Break;
}
}
Jspace rewrite
Location ~ . * \. Php? $
{
# Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}
Location ~ * ^/Index. php/
{
Rewrite ^/index. php/(. *)/index. php? $1 break;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}