Question on rewriting ...

Maxim Dounin mdounin at
Thu Sep 6 00:30:35 MSD 2007


On Wed, 5 Sep 2007, Rakhesh Sasidharan wrote:

>> This is what you wrote in your config. If you need redirect, not internal 
>> rewrite, use:
>> rewrite ^/feed/.+$   /index.php      redirect;
>> Note "redirect" flag. It's default for substitute starting with "http://", 
>> but must be explicitly specified for others.
> That's how I finally have it working now. By using a redirect.

Oops, sorry, looks like I misread your message. Your problem is that 
WordPress get's wrong url and confused, right?

To debug this, you should defenitely look at the place where you pass 
request to php (e.g. "location ~ \.php$" in your nginx.conf) and 
(probably) your WordPress configs/code.

> If I can have a rule to rewrite /feed/(rss|atom|rdf|rss2) URLs to 
> /?feed=<whatever> then can't I have a rule to rewrite /feed/<anything else> 
> to /?feed=rss? Why would it not work? (I used /index.php in my example above, 
> but I also tried with /?feed=rss and it didn't work).

Just tried this with fresh WordPress installation - it just worked with 
(almost) default configuration. The config follows:

     server {
         listen 80;
         server_name skipped;

         root /usr/local/www/data/wordpress;

         location / {
             index index.php;

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

         location ~ \.php$ {
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
             include fastcgi_params;

The only difference in "location ~ \.php$" from the example in default 
nginx config is SCRIPT_FILENAME (in example it's 

The fastcgi_params file - default as shipped with nginx:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

With this config, it returns rss feed for /feed/rss, atom feed for 
/feed/atom, and rss feed again for /feed/no-such-feed. Looks like what you 
are trying to achive.

Versions: WordPress 2.2.2, nginx 0.6.10.

Happy debugging!

Maxim Dounin

More information about the nginx mailing list