Home>

Introduction:nginx rewrite rewrite rules and anti-theft chain configuration methods,Several forms of the flag tag in the rewrite rule format,When stealing a link, a 403 error is returned. The allowed domain name immediately follows the domain name in the second line.

The nginx rewrite rewrite rules and anti-theft chain configuration methods are as follows:

nginx rewite rules, official documentation:

nginx rewrite rule format:rewrite regex replacement flag

The flag has four formats:

last-equivalent to l in apache

break-abort rerewite without continuing to match

redirect-returns a temporarily redirected http status 302, equivalent to r in apache

permanent-returns the HTTP status 301 for permanent redirection, which is equivalent to r=301 in Apache

Can be placed in server, location and if modules.

Match judgment:

~ Is case-sensitive matching; ! ~ Is case insensitive

~ * Is case-insensitive matching;! ~ Is case insensitive

For example, set nginx to redirect users to use/ie to the/nginx-ie directory:

if ($http_user_agent ~ msie) {
rewrite ^ (. *) $/msie/$1 break;
}

Attached, commonly used nginx rewrite rule configuration code.

1. Use only one URL,For example, the main URL is set to.

if ($host!="www.xfcodes.com") {
rewrite ^/(. *) $http://www.xfcodes.com/$1 permanent;
}

When you visit xfcodes.com, you will be redirected automatically.

Anti-theft chain

location ~ *. (gif|jpg|png|swf|flv) ${
valid_referers none blocked xfcodes.com dgjs123.com;
if ($invalid_referer) {
return 403;
}
}

When stealing a link, a 403 error is returned, and the allowed domain name can immediately follow the domain name in the second line.

3, wordpress rewrite

location/{
index index.html index.php;
if (-f $request_filename/index.html) {
rewrite (. *) $1/index.html break;
}
if (-f $request_filename/index.php) {
rewrite (. *) $1/index.php;
}
if (! -f $request_filename) {
rewrite (. *) /index.php;
}
}

Currently, this code is used in the code collection.

4.bo-blog nginx rewrite rules under nginx

if (! -e $request_filename) {
rewrite ^/post/([0-9] +) /?([0-9] +)?/?([0-9] +)?/?$/read.php?entryid=$1&page=$2&part=$3 last;
rewrite ^/page/([0-9] +)/([0-9] +) /?$/index.php?mode=$1&page=$2 last;
rewrite ^/starred/([0-9] +) /?([0-9] +)?/?$/star.php?mode=$1&page=$2 last ;
rewrite ^/category/([^ /] +) /?([0-9] +)?/?([0-9] +)?/? ;$/index.php?go=category_$1&mode=$2&page=$3 last;
rewrite ^/archiver/([0-9] +)/([0-9] +) /?([0-9] +)?/?([0-9] +)?/?$/index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
rewrite ^/date/([0-9] +)/([0-9] +)/([0-9] +) /?([0-9] +)?/?([0-9] +)?/?$/index.php?go=showday_ $1- $2- $3&mode=$4&page=$5 last;
rewrite ^/user/([0-9] +) /?$/view.php?go=user_$1 last;
rewrite ^/tags/([^ /] +) /?([0-9] +)?/?([0-9] +)?/? ;$/tag.php?tag=$1&mode=$2&page=$3 last;
rewrite ^/component/id/([0-9] +) /?$/page.php?pageid=$1 last;
rewrite ^/component/([^ /] +) /?$/page.php?pagealias=$1 last;
#force redirection for old rules
rewrite ^/read \ .php/([0-9] +) \. htm $http://$host/post/$1/permanent;
rewrite ^/post/([0-9] +) \. htm $http://$host/post/$1/permanent;
rewrite ^/post/([0-9] +) \ _ ([0-9] +) \. htm $http://$host/post/$1/$2/permanent;
rewrite ^/post/([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/post/$1/$2/$3/permanent;
rewrite ^/index \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/page/$1/$2/permanent;
rewrite ^/star \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/starred/$1/$2/permanent;
rewrite ^/category \ _ ([0-9] +) \. htm $http://$host/category/$1/permanent;
rewrite ^/category \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/category/$1/$2/$3/permanent;
rewrite ^/archive \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/archiver/$1/$2/permanent;
rewrite ^/archive \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/archiver/$1/$2/$3/$4/permanent;
rewrite ^/showday \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \. htm $http://$host/date/$1/$2/$3/permanent;
rewrite ^/showday \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0- 9] +) \. Htm $http://$host/date/$1/$2/$3/$4/$5/permanent;
#filename alias
rewrite ^/([a-za-z0-9 _-] +) /?([0-9] +)?/?([0-9] +)?/?$/read.php?blogalias=$1&page=$2&part=$3 last;
}
  • Previous Setting user preferences using the Preferences API in Java
  • Next JQ selector_implementation method of selecting the Nth child element of the same element