Nginx url rewrite rule

mod-rewritenginxrewrite

I'm doing a migration from a WordPress blog to a Jekyll Blog using NGINX as my HTTP server and I don't want to break the Internet by leaving some URL's not working as expected. I got to a point where I really need to ask for help.

The original WordPress blog URLs are like this:

example.com/series/

example.com/series/some-stuff/

I want to mimic this urls by removing the file extension (.html) and respond with a non 404 page if someone has stored the WordPress URLs in his bookmarks. The Jekyll default URLs without any rewrites are like this:

example.com/series/ --> This one is correct by default.

example.com/series/some-stuff.html --> Not ok.

As you can see the first URL, series section its already ok because it's the index.html page and Jekyll hides de file name in that case (as expected), but when you access any other page in the same section (/series) you get the full file name with extension. Ok, so, I started working in the rewrites to remove the extension and be able to respond to the same wordpress URL and got here:

rewrite ^/series/(.*)\.html*$ /series/$1 permanent;
rewrite ^/series/(.*)/$ /series/$1 permanent;

example.com/series/ redirects to example.com/series/index --> It's working ok but I don't like that now it's showing me the file name (index).

The other ones are working as I expect and are responding to the old WordPress URls.

example.com/some-stuff.html redirects to example.com/series/some-stuff --> Correct!

example.com/series/some-stuff/ redirects to example.com/series/some-stuff --> Correct!

As you can see I've manage to make this URLs work but with the exception of adding the file name (index) to the main series page. All I want is to do is fix the URL that I "broke" with the rewrites (example.com/series/index) but keep the others as they are after the rewrite. Thanks!

edit:

I tried to make a rewrite just for example.com/series/index doing this:

rewrite ^/(series/index.html$|series/index$) /series/ permanent;

But it didn't worked all I get is 404s.

I also tried to do a location + alias for that URL and got a 404 also.

Best Answer

The question is very poorly formatted, with obvious mistakes in content, inconsistencies between the lines, and omission of other relevant rewrite rules to shed the light onto the complete and full picture, so, I'm not sure a correct answer could be provided.

However, the general idea is that there is a difference between permanent and redirect rewrites on the one hand, which rewrite the URL as visible to the user, and internal rewrites that rewrite the URL within the server itself, on the other.

So, what you really want here is an internal rewrite of the user-visible URL back to what it actually is internally. Maybe something like this, and remove any other rewrites for /series/index:

rewrite ^/series/$ /series/index last;
if ($request_uri = /series/index) {  return 301 /series/;  }