Nginx rewrite rewrite and anti-theft chain configuration

Source: Internet
Author: User

Nginx rewrite rewrite rules and anti-theft chain configuration method
time: 2016-02-04-15:16:58 Source: Network
Introduction: Nginx rewrite rewrite rules and anti-theft chain configuration method, rewrite rule format in several forms of flag flag, hotlinking return 403 error, the allowed domain name directly following the second line of the domain name. nginx Rewrite rewrite rules and anti-theft chain configuration method

Nginx Rewite Rules, official documents: Http://wiki.nginx.org/NginxHttpRewriteModule

Nginx rewrite rule format: rewrite regex replacement flag

The flag tag is available in four formats:

last– equivalent to the L in Apache.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status 302 for temporary redirection, equivalent to the R in Apache
permanent– returns the HTTP status 301 for permanent redirection, which is equivalent to r=301 in Apache

Can be placed in the server, location, and if modules.

Match judgment:
~ Case-sensitive matching;!~ is case-sensitive
~* for case-insensitive matching;!~ for case insensitive

For example, set Nginx to redirect to the/nginx-ie directory when the user uses IE:

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 site is set to www.xfcodes.com.

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

When you access xfcodes.com, it automatically jumps to www.xfcodes.com.

2, anti-theft chain

Location ~*. (gif& #124;jpg& #124;p ng& #124;swf& #124; flv) $ {
Valid_referers none blocked xfcodes.com dgjs123.com;
if ($invalid _referer) {
return 403;
}
}

A 403 error is returned when hotlinking, allowing the domain name to be followed directly from the second row of the domain name.

3, the rewrite of WordPress

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;
}
}

At this time, the Code collection is the code used.

4.bo-blog in Nginx under Nginx rewrite rules

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;
}

Nginx rewrite rewrite and anti-theft chain configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.