Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
I convert this .htaccess rewrite rule:
Code: Select all
RewriteRule ^news$ "$0/" [R=301,L,QSA]
RewriteRule ^news/$ news.php [L,QSA]
RewriteRule ^news/.*?([^\.\/]*)\.html$ news.php?pname=$1 [L,QSA]
RewriteRule ^news/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ news.php?d=$1$2$3 [L,QSA]
RewriteRule ^news/[^\.]*?([^/\.]*)/$ news.php?fname=$1 [L,QSA]
RewriteRule ^news/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

Into Nginx rewrite rule:
Code: Select all
rewrite ^/news$ /news/ permanent;
#301 redirect from "/news" to "/news/" --WORKS

rewrite ^/news/$ /news.php last;
#if uri "/news/" process by "/news.php" --WORKS

rewrite ^/news/.*?([^./]*).html$ /news.php?pname=$1 last;
#if example uri "/news/politic/fresh-political-news.html" should be process by "/news.php?pname=fresh-political-news" but it DOESN'T WORKS

rewrite "^/news/([1-2]d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /news.php?d=$1$2$3 last;
#I guess this is for archive by date. if example uri "/news/2015/05/30/" should be process by "/news.php?d=20150530" but it DOESN'T WORKS

rewrite ^/news/[^.]*?([^/.]*)/$ /news.php?fname=$1 last;
#if example uri "/news/politic/" process by "/news.php?fname=politic" --WORKS

rewrite ^/news/[^.]*?([^/.]*)$ /news/$1/ permanent;
#301 redirect from example uri "/news/politic" to "/news/politic/" --WORKS

NB: Note the comment


I have spent more than 4 hours to find a solution without luck.
Could someone that familiar with Nginx and Regular expression help me with this rewrite please....
Hi littlekoala,

I had the opportunity to help a user with getting prettyURLs running on Nginx a while back.

The conversation all happened via PM though so it is not on the main forum.
I'll paste here the main portions of the conversation. Perhaps you'll find it useful -

My final reply:

Code: Select all
I've tested on Winginx and the following seems to be working fine for me. The marked out rules below in 'location / ' block within 'server' block are the ones that you should try adding to the same place in your conf file.
Code:
server {
    listen   127.0.0.1:80;

    ..
    ..
   
    location / {
       # existing rules
        ..
        ..
       
        ###### Couch templates start #####
        if (!-e $request_filename) {
            #services.php
            rewrite ^/services$ /services/ permanent;
            rewrite ^/services/$ /services.php last;
            rewrite ^/services/.*?([^\.\/]*)\.html$ /services.php?pname=$1 last;
            rewrite "^/services/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /services.php?d=$1$2$3 last;
            rewrite ^/services/[^\.]*?([^/\.]*)/?$ /services.php?fname=$1 last;

            #portfolio.php
            rewrite ^/portfolio$ /portfolio/ permanent;
            rewrite ^/portfolio/$ /portfolio.php last;
            rewrite ^/portfolio/.*?([^\.\/]*)\.html$ /portfolio.php?pname=$1 last;
            rewrite "^/portfolio/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /portfolio.php?d=$1$2$3 last;
            rewrite ^/portfolio/[^\.]*?([^/\.]*)/?$ /portfolio.php?fname=$1 last;
           
            #blog.php
            rewrite ^/blog$ /blog/ permanent;
            rewrite ^/blog/$ /blog.php last;
            rewrite ^/blog/.*?([^\.\/]*)\.html$ /blog.php?pname=$1 last;
            rewrite "^/blog/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /blog.php?d=$1$2$3 last;
            rewrite ^/blog/[^\.]*?([^/\.]*)/?$ /blog.php?fname=$1 last;

            #index.php
            rewrite ^/.*?([^\.\/]*).html$ /?pname=$1 last;
            rewrite "^/([1-2]d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /?d=$1$2$3 last;
            rewrite ^/[^\.]*?([^/\.]*)/$ /?fname=$1 last;
       
        }
        ####End Couch templates ######
    }
   
    ..
    ..
   
}

User's final reply:
Didn't think it had worked, but it was due to me adding it to default within /sites-available/ and not updating the system link to sites-enabled, so I created a copy of default in /sites-available/ and renamed to a different name, pasted the Couch templates within location / and made a system link, restarted NGINX via SSH and it worked.

Absolutely amazing, both I and the client can't thank you enough.

Not sure if needed, but I know a lot of people a moving to NGINX nowadays, so IMO, I think this would make a great add-on to the PrettyURL's post on how to do it on NGINX, as there is already one for Apache.

Hope it helps.
Could you please update this by taking account the new multi-lang plugin, which requires language tags in the urls? It would be greatly beneficial, if the "gen_htaccess" script could output nginx rules, so we don't have to ask in the forum :) Thank you.
3 posts Page 1 of 1

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 2 guests

cron