0

So, coming from the Apache world I am new to configuring NGNIX more than just the root site. I have tried to use an Apache to NGNIX rewrite, but the output doesn't seem to render the pages correctly.

Original .htaccess code:

RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

The output doesn't seem to work after reloading the new configuration.

I have been trying different configurations under my location block trying to just add .html and end of the URI/URL. This is for a sub-directory called "wiki" just serving static HTML files.

Here's my current configuration under HTTPS:

root /var/www/html/;
index index.php index.html;

...
    location ^~ /wiki/ {
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^(.+)/$ $1.html permanent;
    }

The alias or path is /var/www/html/wiki

Please advise. I was able to get /wiki/index.html to render, but any other HTML files do not rewrite from wiki/product to product/product.html. I keep getting 404 errors.

  • Try `try_files $uri $uri.html $uri/ @rewrite;` or `rewrite ^(.+?)/?$ $1.html permanent;` – Ivan Shatsky Sep 27 '21 at 20:37
  • Okay, ```rewrite ^(.+?)/?$ $1.html permanent;``` worked, and I see there was an "?". Why did this work? On another note, thank you very much @IvanShatsky! – Kevin Keeney Sep 27 '21 at 20:40
  • `/?` makes trailing slash presence optional. Your previous `^(.+)/$` makes a trailing slash mandatory and match `/wiki/product/` but not `/wiki/product`. `(.+?)` is a [lazy](https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions) quantifier. Without it `/wiki/product/` will be rewrited to `/wiki/product/.html` instead of `/wiki/product.html` – Ivan Shatsky Sep 27 '21 at 20:45

0 Answers0