Nginx重写规则,有些工作,有些则不行

这是两个重写规则:

这个工作

rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last; 

这一个不

 rewrite ^/announcements/([0-9]+)/[a-z0-9_-]+.html$ /./announcements.php?id=$1 last; 

就我所知,两者没有区别。 公告要重写的url是:

 /announcements/2/New-Site-Design.html 

而且应该改写成:

 /announcements.php?id=2 

与知识库相比,我实在看不出公告是如何工作的。 任何提示将非常感谢。

===添加configuration文件 – 我的域名

 #HTTP server { listen 80; server_name portal2.website.co.uk website.co.uk www.website.co.uk; access_log /var/log/nginx/website.access_log; error_log /var/log/nginx/website.error_log; root /var/www/website.co.uk/www; index index.php index.htm index.html; location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/website.co.uk/www$fastcgi_script_name; include fastcgi_params; } rewrite ^/announcements$ /./announcements.php last; rewrite ^/announcements/([0-9]+)/[a-z0-9_-]+.html$ /./announcements.php?id=$1 last; rewrite ^/knowledgebase$ /./knowledgebase.php last; rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last; rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last; rewrite ^/downloads$ /./downloads.php last; rewrite ^/downloads/([0-9]+)/([^/]*)$ /./downloads.php?action=displaycat&catid=$1 last; } 

在上面的configuration文件中,我用“网站”replace了对我的域的引用

===编辑

这是错误日志行

 2012/06/28 11:40:04 [error] 29095#0: *7 open() "/var/www/website.co.uk/www/announcements/6/Moving-to-a-new-server.html" failed (2: No such file or directory), client: 92.232.232.25, server: portal2.website.co.uk, request: "GET /announcements/6/Moving-to-a-new-server.html HTTP/1.1", host: "portal2.website.co.uk" 

所以重写规则只是没有在/announcements/id/page-title.html中踢脚

Moving-to-a-new-server包含资本M ,最后我记得,重写规则是区分大小写的。

我正在猜测,但是当你用knowledgebase进行testing时,只能以小写forms提供后一部分。

如果是的话,试试这个:

 rewrite ^/announcements/([0-9]+)/[a-zA-Z0-9_-]+.html$ /./announcements.php?id=$1 last; 

并为knowledgebase做类似的更改。 你可以使用.*? 而不是第二部分的具体expression式,因为你实际上并不打算在任何地方使用它(尽pipe它会匹配更多的符号)。

周杰伦在关于首都M回答中是正确的。 写入知识库的原因是因为这是匹配的重写:

rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last;

而不是这个:

rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last;

[^/]是否定匹配,意思是匹配匹配/所有内容,这就解释了为什么你的请求改写了/knowledgebase/1/General-Terms-of-Service.html ,而不是/announcements/2/New-Site-Design.html