Here are the two rewrite rules:

This one works

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

This one doesn't

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

There is no difference between the two as far as I can see. The url to be rewritten for announcements is:


And should be rewritten to:


I really can't see how the announcements one doesn't work compared to the knowledgebase one. Any tips would be greatly appreciated.

=== Added config file - my domain

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_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;


In the config file above I have replaced references to my domain with 'website'

=== Edit

Here is the error log line

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:, server: portal2.website.co.uk, request: "GET /announcements/6/Moving-to-a-new-server.html HTTP/1.1", host: "portal2.website.co.uk"

So the rewrite rule is just not kicking in for /announcements/id/page-title.html

  • Is there any other rewrites between these two? Are they in the same virtualhost or location-block? Can you post your entire configuration? – pkhamre Jun 28 '12 at 8:52
  • Everything in the config seems to work apart from the announcements.php?id=$1 rewrite. Thanks for any help – Lawrence Goldstien Jun 28 '12 at 9:24

Moving-to-a-new-server contains a captial M, and last I remember, rewrite rules are case-sensitive.

I'm taking a guess, but when you have been testing it with knowledgebase you have only supplied the latter portion in lower case.

If so, try this:

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

and make a similar change for knowledgebase. You could just use .*? instead of a specific expression for the 2nd part, since you aren't actually going to use it anywhere (it would match more symbols, though).

  • /knowledgebase/1/General-Terms-of-Service.html Is the url for knowledgebase so no dice :-( – Lawrence Goldstien Jun 28 '12 at 11:12
  • Right we seem to be in business, the knowledge base one did not rely on the bit after however the announcements one. Changed and fixed. – Lawrence Goldstien Jun 28 '12 at 11:18
  • This is the rewrite working for your knowledgebase urls: rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last; So its not this one that rewrites: rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last; – pkhamre Jun 28 '12 at 11:19
  • well no it is, the first one listed is for categorys where there is no .html on the end of the URL. The second one rewrites to article as the url has .html on the end ;-) – Lawrence Goldstien Jun 28 '12 at 11:24
  • You are wrong. ([^/])* will match "Foobar.html" which [a-z0-9_-]+.html won't match. – pkhamre Jun 28 '12 at 12:18

Jay is correct in his answer about the capital M. The reason why the write works on knowledge base is because this is the rewrite that is matched:

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

And not this one:

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

[^/] is a negative match, which means to match everything not matching /, which explains why your request to /knowledgebase/1/General-Terms-of-Service.html was rewritten, but not /announcements/2/New-Site-Design.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.