duanpiao6679 2015-06-23 12:57
浏览 65
已采纳

Nginx wordpress url重写,另有规则

I'm new to nginx and I'm configuring my server block. Currently everything works fine but now I'm trying to move my wordpress website that was on an Apache server. On my website I had a rewriting rule that allowed me to access a php page without specifying the .php extension. I had also the wordpress permalink rewriting rule.

Now on nginx, I managed to make them work but only separately:

location / { # Accessing php script without specifying the extension try_files $uri $uri/ $uri.html $uri.php?$query_string; }

...and...

location / { # Wordpress permalinks try_files $uri $uri/ /index.php?q=$uri&$args; }

What I would want to do, is making this two rules work together. I have no idea how to do it and I didn't find anything that corresponded to what I wanted.

Thanks very much !

  • 写回答

2条回答 默认 最新

  • dongshi2458 2015-06-23 20:24
    关注

    If I understand your use case correctly, you should be able to combine the two into one location block like this:

    location / {
      try_files $uri $uri/ $uri.html $uri.php /index.php?q=$uri&$args;
    }
    

    You'll still need a separate location block after that to process PHP. Something like:

    location ~ \.php$ {
      #match actual filename with extension or file not found
      try_files $uri =404;
    
      #... or replace next lines with your preferred PHP processing config
      include fastcgi_params;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
    

    Of course, this is not the complete configuration but hopefully helps with this aspect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?