0

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?

2 Answers 2

0

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. :)

0

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.

2
  • 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 Commented Aug 22, 2013 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. Commented Aug 22, 2013 at 13:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.