301 redirect can not only make the page automatically jump, but also may pass PR value for search engines

  1. Point multiple domain names to the same web directory:

  server_name www.php100.com php100.com;

  rewrite ^/$ / redirect;

  2. Turn the domain name 301 without www to the domain name with www:

  server_name www.php100.com php100.com;

  if ( $host != "www.php100.com" ) {

  rewrite ^/(.*)$ http://www.php100.com/$1 permanent;

  }

  Detailed introduction of nginx redirection rules

  rewrite command

  nginx's rewrite is equivalent to apache's rewriterule (in most cases, the original apache rewrite rules can be used directly with quotation marks), and it can be used in server, location and IF condition judgment blocks. The command format is as follows:

  rewrite regex to replace target flag tag

  The flag tag can be in the following formats:

  last - basically use this Flag.

  break - abort Rewirte, not continue matching

  redirect – returns HTTP status 302 for temporary redirection

  permanent – ​​returns HTTP status 301 for permanent redirects

  For example, the following paragraph sets nginx to redirect files under a directory to another directory, and $2 corresponds to the corresponding string in the second bracket (.*):

  location /download/ {

  rewrite ^(/download/.*)/m/(.*)\..*$ $1/nginx-rewrite/$2.gz break;

  }

  IF condition judgment of nginx redirection

  In the two cases of server and location, the IF condition of nginx can be used to judge, and the conditions can be as follows:

  regular expression

  Such as:

  match judgment

  ~ is a case-sensitive match; !~ is a case-sensitive mismatch

  ~* is a case-insensitive match; !~ is a case-insensitive mismatch

  For example, the following sets nginx to redirect to the /nginx-ie directory when users use ie:

  if ($http_user_agent ~ MSIE) {

  rewrite ^(.*)$ /nginx-ie/$1 break;

  }

  File and directory judgment

  -f and !-f determine whether a file exists

  -d and !-d determine whether a directory exists

  -e and !-e determine whether a file or directory exists

  -x and !-x determine whether the file is executable

  For example, the following sets nginx to redirect files and directories that do not exist:

  if (!-e $request_filename) {

  proxy_pass http://127.0.0.1/;

  }

  return

  Return http code, such as setting nginx anti-leech:

  location ~* \.(gif|jpg|png|swf|flv)$ {

  valid_referers none blocked http://www.php100.com/ http://www.php100.cc/;

  if ($invalid_referer) {

  return 404;

  }

  }

  set

  set nginx variable

  301 redirect method

  301 redirection is performed, www.php100.com and php100.com are merged, and the previous domain name is also merged. There are two implementation methods, the first method is to judge the nginx core variable host (the old version is http_host) :

  server {

  server_name www.php100.com php100.com ;

  if ($host != 'www.php100.com) {

  rewrite ^/(.*)$ http://www.php100.com/$1 permanent;

  }

  ...

  }

  The second method:

  server {

  server_name php100.com;

  rewrite ^/(.*) http://www.php100.com/$1 permanent;

  }

  The first method is tested ok. Among these two methods, permanent is the key. For details, see the description of nginx redirection rules.

  last - basically use this Flag.

  break - abort Rewirte, not continue matching

  redirect – returns HTTP status 302 for temporary redirection

  permanent – ​​returns HTTP status 301 for permanent redirects

  Ok, now can check the result

  The second method did not test successfully...

  Detailed description of nginx rewrite pseudo-static configuration parameters (transfer)

  http://hi.baidu.com/hx10/blog/item/942a0ad784f3ffd0a144df94.html

  nginx rewrite pseudo-static configuration parameters and usage examples with regular usage instructions

  Regular expression match where:

  * ~ for case sensitive matching

  * ~* is a case-insensitive match

  * !~ and !~* are case sensitive mismatch and case insensitive mismatch respectively

  File and directory match where:

  * -f and !-f are used to determine whether a file exists

  * -d and !-d are used to determine whether a directory exists

  * -e and !-e are used to determine whether a file or directory exists

  * -x and !-x are used to determine whether the file is executable

  The flags are marked with:

  * last is equivalent to the [L] mark in Apache, indicating that the rewrite is completed

  * break terminates the match, no longer matches the following rules

  * redirect returns 302 temporary redirection The address bar will display the redirected address

  * permanent return 301 permanent redirect address bar will display the redirected address

  Some available global variables are available, which can be used for conditional judgment (to be completed)

  $args

  $content_length

  $content_type

  $document_root

  $ documents_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 with 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 into 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, the following sets nginx to redirect to the /nginx-ie directory when users use ie:

  if ($http_user_agent ~ MSIE) {

  rewrite ^(.*)$ /nginx-ie/$1 break;

  }

  The directory automatically adds "/"

  if (-d $request_filename){

  rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

  }

  disallow htaccess

  location ~/\.ht {

  deny all;

  }

  Disallow multiple directories

  location ~ ^/(cron|templates)/ {

  deny all;

  break;

  }

  Disallow files starting with /data

  Requests such as .log.txt in multi-level directories under /data/ can be prohibited;

  location ~ ^/data {

  deny all;

  }

  Forbid a single directory

  Can not prohibit .log.txt can request

  location /searchword/cron/ {

  deny all;

  }

  Ban individual files

  location ~ /data/sql/data.sql {

  deny all;

  }

  Set expiration time for favicon.ico and robots.txt;

  Here, favicon.ico is 99 days, robots.txt is 7 days and no 404 error log is 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; here is 600 seconds, and access logs are not recorded

  location ^~ /html/scripts/loadhead_1.js {

  access_log off;

  root /opt/lampp/htdocs/web;

  expires 600;

  break;

  }

  File anti-hotlinking and set expiration time

  The return 412 here is a custom http status code, the default is 403, which is convenient to find the correct hotlink request

  "rewrite ^/ http://leech.c1gstudio.com/leech.gif;" shows a picture of anti-leech

  "access_log off;" does not record access logs, reducing stress

  "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 allow a fixed IP 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 the files in the multi-level directory into one file to enhance the seo effect

  /job-123-456-789.html points 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 the second-level directory

  For example, /shanghaijob/ points to /area/shanghai/

  If you change last to permanent, the browser address bar shows /location/shanghai/

  rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

  The problem with the above example is that accessing /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 links in the page cannot be used.

  For example, the real address of ./list_1.html is /area /shanghia/list_1.html will become /list_1.html, which will lead to inaccessibility.

  Then I can't 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;

  }

  It's easy to do after knowing the reason, let me jump manually

  rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

  rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

  Redirect when files and directories do not exist:

  if (!-e $request_filename) {

  proxy_pass http://127.0.0.1/;

  }

  domain name 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;

  }

  Multiple domain forwarding

  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 name jump

  if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {

  rewrite ^(.*) http://top.yingjiesheng.com$1/;

  break;

  }

  Domain mirroring

  server

  {

  listen 80;

  server_name mirror.c1gstudio.com;

  index index.html index.htm index.php;

  root /opt/lampp/htdocs/www;

  rewrite ^/(.*) http://www.c1gstudio.com/$1 last;

  access_log off;

  }

  Mirror a subdirectory

  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 separate domain name for a section of 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;

  }

  }

  Discuz ucenter avatar rewrite optimization

  location ^~ /ucenter {

  location ~ .*\.php?$

  {

  #fastcgi_pass unix:/tmp/php-cgi.sock;

  fastcgi_pass 127.0.0.1:9000;

  fastcgi_index index.php;

  include fcgi.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;

  include fcgi.conf;

  }

  location ~* ^/index.php/

  {

  rewrite ^/index.php/(.*) /index.php?$1 break;

  fastcgi_pass 127.0.0.1:9000;

  fastcgi_index index.php;

  include fcgi.conf;

  }