I have Apache rules for a Web application that look like so:

RewriteEngine On
RewriteRule ^unsubscribe/([-A-z0-9]+)/?(.*)$ /list/unsubscribe.php?id=$1&why=$2 [L,QSA]
RewriteRule ^unsub/([-A-z0-9]+)/?(.*)$ /list/subscribe.php?id=$1&why=$2 [L,QSA]

I've tried:

rewrite ^unsubscribe/([-A-z0-9]+)/?(.*)$ /list/unsubscribe.php?id=$1&why=$2;

Which didn't seem to match and results in a 404.

    location /unsubscribe {
        rewrite ^/unsubscribe/([-A-z0-9]+)/?(.*)$ /list/unsubscribe.php?id=$1&why=$2 break;

Results in serving the file, but without PHP processed http://sg.hackandtell.org/unsubscribe/foo/bar

Perhaps can someone recommend some debug strategies?


I don't have a debug strategy, so I am not 100% sure what nginx is doing. Though I rewrote the rewrite rules like so:

rewrite ^/unsubscribe/([a-zA-Z0-9]*)$ /list/unsubscribe.php?id=$1;
rewrite ^/unsub/([a-zA-Z0-9]*)$ /list/subscribe.php?id=$1;
rewrite ^/unsubscribe/([a-zA-Z0-9]*)/(.*)$ /list/unsubscribe.php?id=$1&why=$2;
rewrite ^/unsub/([a-zA-Z0-9]*)/(.*)$ /list/subscribe.php?id=$1&why=$2;

And it works. :)


The nginx Wiki states:

last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.

break - completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.

So when using break, it will use the server {...}-wide root directive (I assume you have one) and serve the file statically.

You should rather try last in your rewrite rule, because then the PHP-processing route will match in a second turn and direct the request to PHP.

  • I added rewrite_log on; & error_log /tmp/rewrite.log notice;. I don't see any need by looking at the log: s.natalian.org/2013-08-22/1377159280_1366x768.png – hendry Aug 22 '13 at 8:15
  • My answer is just an alternative to yours and mainly for the purpose of better understanding the rewrite flags in nginx (break vs. last) because there's often confusion about what they do. – Lukas Aug 22 '13 at 13:32

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.