zenPhoto rewrite rules for nginx

Written on 05/06/2012 by Patrick Bregman


I am setting up zenPhoto for a new project, and it isn't exactly nice to have every single URL contain index.php. For Apache, there are a load of rewrite rules included with the project. Unfortunately, they didn't include nginx rules, which is kinda understandable. Because if you include nginx rules, then you should also include LigHTTPD and a lot of other webservers. With a bit of searching I found this page on the nginx wiki. While the rules appeared to work just fine, I noticed the rules for pages and news were missing.

With a little bit of studying I discovered how to make my own rewrite rules for nginx from the ones that were supplied for Apache, so I decided to rewrite all the useful Apache rules to nginx. I skipped the RSS rules because of two reasons. First and foremost, they were too hard to do at this point in time. And secondly, they aren't necessary as far as I can see. So without further ado, I present to you an updated ruleset for zenPhoto under nginx!

# Tiny URLs
rewrite ^/tiny/([0-9]+)/?$ /index.php?p=$1&t last;
rewrite index\.php$ /index.php last;
rewrite ^/page/([0-9]+)/?$ /index.php?page=$1 last;
rewrite ^/page/([A-Za-z0-9\-_]+)/?$ /index.php?p=$1 last;
rewrite ^/page/([A-Za-z0-9_\-]+)/([0-9]+)/?$ /index.php?p=$1&page=$2 last;
rewrite ^/(.*)/page/([A-Za-z0-9_\-]+)/?$ /index.php?album=$1&p=$2 last;

# Search
rewrite ^/page/search/fields([0-9]+)/(.*)/([0-9]+)/?$ /index.php?p=search&searchfields=$1&words=$2&page=$3 last;
rewrite ^/page/search/fields([0-9]+)/(.*)/?$ /index.php?p=search&searchfields=$1&words=$2 last;
rewrite ^/page/search/archive/(.*)/([0-9]+)/?$ /index.php?p=search&date=$1&page=$2 last;
rewrite ^/page/search/archive/(.*)/?$ /index.php?p=search&date=$1 last;
rewrite ^/page/search/tags/(.*)/([0-9]+)/?$ /index.php?p=search&searchfields=tags&words=$1&page=$2 last;
rewrite ^/page/search/tags/(.*)/?$ /index.php?p=search&searchfields=tags&words=$1 last;
rewrite ^/page/search/(.*)/([0-9]+)/?$ /index.php?p=search&words=$1&page=$2 last;
rewrite ^/page/search/(.*)/?$ /index.php?p=search&words=$1 last;

# Pages
rewrite ^/pages/?$ /index.php?p=pages last;
rewrite ^/pages/(.*)/?$ /index.php?p=pages&title=$1 last;

# News
rewrite ^/news/?$ /index.php?p=news last;
rewrite ^/news/([0-9]+)/?$ /index.php?p=news&page=$1 last;
rewrite ^/news/category/(.*)/([0-9]+)/?$ /index.php?p=news&category=$1&page=$2 last;
rewrite ^/news/category/(.*)/?$ /index.php?p=news&category=$1 last;
rewrite ^/news/archive/(.*)/([0-9]+)/?$ /index.php?p=news&date=$1&page=$2 last;
rewrite ^/news/archive/(.*)/?$ /index.php?p=news&date=$1 last;
rewrite ^/news/(.*)/?$ /index.php?p=news&title=$1 last;

# Images and stuff
rewrite "^/(.*)/image/(thumb|[0-9]{1,4})/([^/\\]+)$" /zp-core/i.php?a=$1&i=$3&s=$2 last;
rewrite ^/(.*)/image/([^/\\]+)$ /zp-core/i.php?a=$1&i=$2 last;
rewrite "^/(.*)/album/(thumb|[0-9]{1,4})/([^/\\]+)$" /zp-core/i.php?a=$1&i=$3&s=$2&album=true last;

# Catch all for unknown stuff
rewrite ^/(.*)/?$ /index.php?album=$1 last;
# End of rules

To use these in zenPhoto, go to the admin panel of zenPhoto, then on the tab Options go to General. Enable mod_rewrite and set the mod_rewrite suffix: to nothing (delete the contents of that field). After this, you'll have nice URLs for zenPhoto in nginx.

UPDATE: I added a stupid "End of rules" line to the end of the rules because I noticed that for most of the rules, the text is too wide for my blog. Make sure you copy it to that line if you want everything. Otherwise, just copy a few characters from the line below the one you want.


blog comments powered by Disqus