Forum

Thread tagged as: Configuration, Blog

Blog clean URL rewrite rules for nginx

Hey guys.

Our live servers use Nginx while our local dev environment uses Apache - so while we're fine with the blog rewrite rules locally, they don't work (obviously) on the Nginx server.

I understand Drew has been investigating using Nginx for routing either to Varnish or the PHP files; but have you gotten around to converting the rules for the blog URLs to Nginx?

crosses fingers

Thanks!

Ben

Ben Furfie

Ben Furfie 3 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

When you say "the rules" - what are you trying to do? The rewrite rules you need will depend entire on how you've structured your site.

Ben Furfie

Ben Furfie 3 points
Registered Developer

Drew McLellan said:

When you say "the rules" - what are you trying to do? The rewrite rules you need will depend entire on how you've structured your site.

It's literally the nginx equivalent of RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L] I need.

The site uses the standard /blog/{postSlug} setting.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Which version of nginx are you using?

Ben Furfie

Ben Furfie 3 points
Registered Developer

It's version 1.6.2

Drew McLellan

Drew McLellan 2638 points
Perch Support

I guess it'd be something like

location /blog/ {
    rewrite ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 break;
}

but that's just from looking at the documentation. I've not tested it.

Ben Furfie

Ben Furfie 3 points
Registered Developer

Thanks Drew.

We tried that but it didn't work properly. The blog page was visible, but the posts weren't.

So we played around a little more:

location /blog/ {
      #try_files $uri $uri/ /blog/post.php?$args;
      #rewrite ^blog/([a-zA-Z0-9-/]+)$ /$1/post.php?s=$2;
      rewrite ^/([^/]*)/(.*)$ /blog/post.php?s=$2;
      index index.php;
}

As it stands, we can get either the blog page or the post page working, but not both at the same time.

Any ideas?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You're asking in the wrong place for support for nginx. I've tried to offer a bit of help, but this really isn't my area.

Ben Furfie

Ben Furfie 3 points
Registered Developer

Thanks for your help Drew; it's much appreciated. I'll keep digging. Once I find the fix, I'll let you know so you can put it in the docs :)

Ben, or anybody for that matter, do you have a working nginx solution for the simple blog rewrite rule please?

I have made a working solution for NGINX rewriting. I have altered the way it works for the Events app, but you could apply this to the Blog:

location /event/ {
        rewrite ^/event/(.*)$ /events/event.php?s=$1 last;
    }

Here what I'm doing is as follows:

Perch Events index page is in /events/index.php

Perch Events event page is in /events/event.php?s=slug

I'm rewriting /event/slug to point to /events/event.php?s=slug

Notice the singluar event (without the 's'). I found I was getting issues redirecting in the same folder, thats why I used /event/ instead.

So now /event/2016-07-14-penchant-16-gullivers-nq

...directs to /events/event.php?s=2016-07-14-penchant-16-gullivers-nq