0

In my website i have links like this:

https://example.com/blog/some-text

So due to the structure of my paths, the code has to read them like this:

https://example.com/blog/_entry.php?title=some-text

What can i do to transform it? For example, with Apache and .htacces you just have to do:

RewriteRule ^blog/([\da-z-]+)$ blog/_entry.php?title=$1 [L,NC]

In nginx i can't get it to work with the server block. I have tried so many different ways but none of then have worked, i tried something like the following:

location /blog {
  rewrite ^/blog/([\da-z-]+)$ /blog/_entry.php?title=$1 break;
}

But when you go to https://example.com/blog/some-text it downloads the file instead of opening it, other solutions have not worked for me either

  • Assuming you have valid PHP handler location in your nginx config (usually something like `location ~ \.php$ { ... fastcgi_pass ... }`), replace `break` flag with the `last`one. `last` flag will force nginx to search a new location for the rewritten URI. `break` flag makes nginx handling rewritten URI within the current location. Since no other content handlers are declared here explicitly (`proxy_pass`, `fastcgi_pass` and other similar directives are exactly those content handler declarations), the default static content handler will be used, resulting in raw PHP script contents in response. – Ivan Shatsky May 26 '22 at 02:08
  • See [this general answer for serving PHP files](https://stackoverflow.com/a/26668444/315024), then place your location before the php one, and replace `break` with `last` in your rule. – Walf May 31 '22 at 05:25

0 Answers0