Configuration solves the problem of the WordPress path not being automatically slashes in the Nginx server

  • 2020-05-10 23:26:46
  • OfStack

The problem is this: I used to put "wp-admin" after the blog address and hit enter to enter the WordPress background, but when I got in, I found that no matter if I clicked on any one of the management subitems, the 1-law 404(can't find the page), I would instantly click it, what is the situation...

Studying the management under 1 item link, found that they are all similar "/ / www. ofstack. com/blog/edit php", the key is that they are less "/ wp admin/" this path, path is wrong, must have 404 bai...

It is easy to know where the problem is, and the answer must be in the redirection rule of Nginx, but I don't know how to change it, but I also know that WordPress official must have solved it, so I still very calm to check the documentation... got it

http://codex.wordpress.org/Nginx

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

Sure enough, I found a solution on codex, which is simply adding 1 line of slash to redirect. The methods are summarized as follows:

1, login shell vim edit Nginx configuration directory (1 a is in/usr/local/nginx conf /) under the "wordpress. conf", of course, if you like I wrote 1 sample with their conf file, then change to the corresponding that redirects the configuration file.


vim /usr/local/nginx/conf/wordpress.conf

2. Just add the official line to the end of the file


location /blog/ {
if ($host != 'www.ofstack.com' ) {
rewrite ^/(.*)$ //www.ofstack.com/$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 (.*) /blog/index.php;
}
}

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

3. Save wq and restart nginx

Actually, if you're lazy, just use "echo. > > "Just append = =

PS: it is said that nginx does not automatically add a slash to the end of a request and does not automatically determine whether a file or a directory is being requested. An online search for nginx slashes will mention the need to add this slash at the end of the request.

Most of the information on the Internet goes like this:

Add nginx\conf\ vhost.conf location / {} index   index.html


if (-d $request_filename) {
 rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}


But if you have a port at the end of your url, you will still have a 403 error when redirecting.

That is, the $host variable has lost the port, so it can be changed to $http_host variable

So you could write it as follows and that would solve everything


if (-d $request_filename) {
 rewrite ^/(.*)([^/])$ http://$http_host/$1$2/ permanent;
}


Related articles: