Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Converting Apache rewrite rules for use in nginx
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Converting Apache rewrite rules for use in nginx

Hello,

I am currently moving a website that was hosted on Apache and is being moved to nginx. I've gotten most rewrite rules converted over and working successfully with the exception of these rules for the website blog (it is pulling announcements from WHMCS and parsing them with SEO friendly URLs). I've tried manually writing the rules into something nginx would understand as well as using some of the web-based converters without success. Are there any nginx gurus out there who could help with these rules:

Existing rules from .htaccess:

RewriteRule ^blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/([0-9]+)/(.*)/?$ - [E=OVERRIDE_REQUEST_URI:/blog,E=BLOG_POST_ID:$1]
RewriteRule ^blog/page/([0-9]+)/?$ - [E=OVERRIDE_REQUEST_URI:/blog,E=BLOG_PAGE_NUM:$1]
RewriteRule ^blog/archives/([0-9]{4})/([0-9]{2})/?$ - [E=OVERRIDE_REQUEST_URI:/blog,E=BLOG_ARCHIVES:$1$2]
RewriteRule ^blog/archives/([0-9]{4})/([0-9]{2})/page/([0-9]+)/?$ - [E=OVERRIDE_REQUEST_URI:/blog,E=BLOG_ARCHIVES:$1$2,E=BLOG_PAGE_NUM:$3]

Thanks in advance for anyone who might be able to help with this, I've been tearing my hair out for the better part of the day trying to get these rules properly converted over.

Comments

  • jarjar Patron Provider, Top Host, Veteran

    A bump purely to say...I feel your pain. Complicated rewrites can almost justify keeping Apache around by itself for some of us. If I could offer any help here it would be to say don't underestimate the value of nginx as a reverse proxy to Apache. Best of both worlds.

    Thanked by 1meaton
  • RalliasRallias Member
    edited November 2014

    Well, straight-forward, if I'm reading this shit right (I probably am not), those would become

    location ~ ^/blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/([0-9]+)/(.*)/?$ {
        fastcgi_param BLOG_POST_ID $1;
        rewrite .* /blog;
    }
    
    location ~ ^/blog/page/([0-9]+)/?$ {
        fastcgi_param BLOG_PAGE_NUM $1;
        rewrite .* /blog;
    }
    
    location ~ ^/blog/archives/([0-9]{4})/(0-9]{2})/?$ {
        fastcgi_param BLOG_ARCHIVES $1$2;
        rewrite .* /blog;
    }
    
    location ~ ^/blog/archives/([0-9]{4})/([0-9]{2})/page/([0-9]+)/?$ {
        fastcgi_param BLOG_ARCHIVES $1$2;
        fastcgi_param BLOG_PAGE_NUM $3;
        rewrite .* /blog;
    }
    

    I'm assuming /blog rewrites somewhere else... you'll want to amend those rules in.

  • Place nginx in front of apache and server static files with it. Although this could be considered another point of failure, that's the best solution for complex rules.

  • Paste full .htaccess, not just a part. This code itself will not work without other lines.

Sign In or Register to comment.