Oct 142011
 

nginxToday i’ve spent a lot of time (probably too much) to understand how to modify extensions, inside an url, with the rewrite of Nginx.

Mi goal was call the url http://mysite.com/miofile.html and serve http://mysite.com/miofile with Nginx, which is actually a php script and not a real html..
To make things more difficult a url with a .html can also specify an existing file and real html file.

Finally i’ve got everything sorted out correctly, so with this post i hope to help someone else in the future to save some time.



While i was searching a solution for my problem i found exactly the opposite, how to add an extension to an url, this was not what i was searching for, but can come handy in otehr situations:

1) Add an extension to an url

Problem: you want to add by default .html to all the urls.

This configuration will rewrite http://xxx.com/myurl to http://xxx.com/myurl.html:

 location / {
         # break if URI has .html extension
         if ($request_filename ~* ^.+.html$) {
           break;
         }
         # add .html to URI and serve file, directory, or symlink if it exists
         if (-e $request_filename.html) {
           rewrite ^/(.*)$ /$1.html last;
           break;
         }
       }

2) Remove an extension from an url

Problem: I had a wordpress site with permalinks that added to the url .html (don’t ask why), but to work properly, url must be treated like any other php.

This solution will change http://xxx.com/myurl.html to http://xxx.com/myurl and send it to the fastcgi process that will parse and serve it to the final user as a php page.

location ~ \.html$ {
               # It stops if the url corresponds to a real .html file
                if (-f $request_filename) {
                expires 30d;
                break;
                }
                #if the file doesn't exists removes the .html extension and continues
                if (!-e $request_filename) {
                rewrite ^/(.*)\.html$ /$1 last;
                }
    }
 
 #standard setup to process an url in Wodpress
 location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php;
 
        if (-f $request_filename) {
                expires 30d;
                break;
        }
        if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php last;
         }
    }
 
location ~ \.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  /srv/www/mysite.com/public_html$fastcgi_script_name;
    }
}

Probably it’s no the most elegant solution, but it works fine, and with this you can have (if you want so), all your wordpress url ending with .html.

Popular Posts:

Flattr this!

  One Response to “How to modify an url extension with a Nginx rewrite”

  1. Nice tip.exactly i was looking for the same.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*