Question on rewriting ...

Rakhesh Sasidharan rakhesh at
Mon Sep 3 11:51:25 MSD 2007


I have http://domain/. Nginx is installed as my webserver, WordPress as 
the blog at /net/store/www/data/wordpress, and here's the relevant bits 
from my config file:

location / {
      root        /net/store/www/data/wordpress;
      index       index.php;

      if (!-e $request_filename) {
          rewrite ^(.*)$  /index.php?q=$1 last;

This config works as expected. I can access my blog at http://domain/, the 
feeds at http://domain/feed/ and so on ... a typical WordPress blog.

Now, for not much reason apart from the fact that I want to play around 
(and learn things), I am trying to setup a rewrite rule such that any 
hits to http://domain/feed/<whatever> (an incorrect URL for feeds) get the 
<whatever> bit stripped out and rewritten to http://domain/feed/.

So I modify the config to the following:

location / {
      root        /net/store/www/data/wordpress;
      index       index.php;

      if (!-e $request_filename) {
         rewrite ^/log/(.*)?$                    /index.php?q=$1 last;
 	rewrite ^/feed/(rss|rss2|atom|rdf)$     /?feed=$1       last;
         rewrite ^/feed/.+$                      /index.php      last;

All my posts are under the /log location (that's part of the permalink 
structure I setup in WordPress). So my idea is to
  * rewrite all hits to http://domain/log to the posts,
  * rewrite all hits to http://domain/feed/(rss|rss2|atom|rdf) to the 
correct feeds, and
  * rewrite all hits to http://domain/feed/<whatever> to 

The first two work as required.

The third one seems to work sort of coz I get my blog page, but with a 404 
error message from WordPress! So that leads me to think the URL is not 
getting rewritten the way I want -- it is getting rewritten but for some 
reason the "/feed/<whatever>" bit too is passed on to index.php. And this 
is the cause for the 404 error.

Any ideas why this is happening the way it is? Strangely enough, when I 
modify the /feed/<whatever> rule to redirect to 
instead of just rewrite, things work! So I figure I've got something wrong 
in my understanding of these rewrite rules ...

Thanks in advance!

 				- Rakhesh

More information about the nginx mailing list