Nginx under WordPress link of url pseudo static 301 permanent redirect implementation method

  • 2020-05-06 12:18:35
  • OfStack

Problem:

The result of the toss in several blog programs Results in two consecutive changes in url. This is the third time.
nginx USES "permanent;" through rewrite. Takes the 301 permanent url redirect.
Previous url structure
//www.jb51.net/post/199/
The url structure
is now required //www.jb51.net/archives/199.html
Process:
Learn the configuration rules of nginx and learn regular expressions (I haven't been exposed to them, either.
nginx Chinese vico: http: / / wiki nginx. org/NginxChs
Introduction to regular expressions: http: / / zh wikipedia. org/zh - cn E5 A3 AD E6 / % % % % % 88% % 99% E8 A1 E5 BE BE E8 A8 % % % % % % BC % 8 F
//www.jb51.net/tools/zhengze.html

Simply explain (seriously learning regular expressions - I didn't seriously learn ^_^) :
^ represents the beginning of the matching string.
$indicates the end of the matching string.
$1 $2 represents the variable
([0-9]+) represents at least 1, at most unlimited number string.
Dizzy (not programmer). . Write the expression.
-- -- -- -- -- -- -- -- -
-- -- -- -- -- -- -- -- -

 
<A class="st_tag internal_tag" title=" The label  rewrite  Under the log " href="http://www.iamle.com/archives/tag/rewrite" rel=tag>rewrite</A> ^/post/([0-9]+)/$ /archives/$1.html permanent; 
<A class="st_tag internal_tag" title=" The label  rewrite  Under the log " href="http://www.iamle.com/archives/tag/rewrite" rel=tag>rewrite</A> ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent; 


Add the expression to the nginx conf file.
-- -- -- -- -- -- -- --, �
location / {
Put here.
}
-- -- -- -- -- -- -- -- -
Completed configuration:

This is my full WordPress nginx rewrite rule configuration.
1. nginx rewrite website url changes 301 redirect.
2, nginx rewrite iamle.cn redirect to nginx.jb51.net.
3. nginx WordPress rewrite pseudo-static rules (able to support custom url of WordPress).

 
location / { 
rewrite ^/post/([0-9]+)/$ /archives/$1.html permanent; 
rewrite ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent; 
if ($host !~ "^www\.iamle\.com$"){ 
rewrite ^(.*) //www.jb51.net$1 permanent; 
} 
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; 
} 
} 


Test results:
Access: / / www. jb51. net/post / 199 / can be jump to / / www jb51. net archives / 199 html


Related articles: