当前位置: 动力学知识库 > 问答 > 编程问答 >

wordpress - Nginx redirect setting

问题描述:

My Nginx setting currently has this:

 location / {

if (!-e $request_filename){

rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect;

}

}

Basically for non-existing pages (404) it redirects user to the home page. But now I have a wordpress blog setup at https://domain.com/blog/, but any wordpress items eg. https://domain.com/blog/test also got redirected to the home page. I wonder how to fix this?

网友答案:

to do something different for requested url's whose path starts with '/blog/' add a corresponding location like so:

location / {
   if (!-e $request_filename){ 
     rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect; }
} 


location /blog/ {
    #add in whatever directives are needed to serve your wordpress
}
网友答案:

You shouldn't be using an if. Please read the IfIsEvil page on the nginx wiki. Instead you should be using try_files.

Your config should look more like this:

location / {
    try_files $uri $uri/ @notfound
}

location @notfound {
    rewrite ^(.*)$ https://domain.com/index.php?id=$1 redirect; 
}

Really, you shouldn't be doing this at all. Instead you should be setting a custom error page. Redirecting every 404 to the home page is bad for your SEO.

Edit: I just realized you were passing the URL into "id". So I removed my second comment about using . instead of ^(.*)$. Still an error page is your best bet. You can pick up the URL with $_SERVER['REQUEST_URI'] (if you avoid the hard redirect).

This page has some sample configurations that may be helpful to you. It has a few wordpress configs.

分享给朋友:
您可能感兴趣的文章:
随机阅读: