Home > Net >  nginx rewrite specific folder to php and arg
nginx rewrite specific folder to php and arg

Time:06-21

Am trying to redirect domain.tld/blog/read.php?article=first-article to domain.tld/blog/first-article

What I tried and didn't work resulting in redirect to domain.tld/first-article

location "^blog/([^/] )/?$" {
 try_files /$uri /$uri/ /blog/read.php?article=$1;
}
location /blog {
 rewrite ^/blog/?$ /blog/read.php?article=? last;
 rewrite ^/blog/([-a-zA-Z0-9_] )/$ /blog/read.php?article=$1? last;
}
location ~ "^/blog/([^/] )/?$" {
 try_files /$uri /$uri/ /blog/read.php?article=$1;
}

Thinking the issue comes from my other parts in the config and mainly second location from below

server {
...
...
location ~ "^/([^/] )/?$" {
 try_files $uri $uri/ /device.php?name=$1;
}

location ~ "^/([^/] )/([^/] )/?$" {
 try_files $uri $uri/ /device.php?name=$1&crversion=$2;
}
...

Any pointers would help a lot Cheers

CodePudding user response:

So the fast answer is actually the fact that my config was fine first time, yet nginx config is read top bottom with first match being the one that is used.
So in the end the "fix" was adding the blog part upper in the site config

location ~ "^/blog/([^/] )/?$" {
 try_files /$uri /$uri/ /blog/read.php?article=$1;
}
  • Related