Reputation: 827
I have read a dozen rewite questions. Most questions are for .php but I have simple .html files so about => about.html .But I am not able to get nginx to do this:
I have tried with this code
rewrite ^/([a-zA-Z0-9]+)$ /$1.html;
but it gives me 404 error on url ending with "/". Also tried this
rewrite ^([a-zA-Z0-9]+)/?$ $1.html
with same error.
Some suggest I should be using try try_files
?
I tried this also which gives a 500 error:
location / {
rewrite ^(.*)$ /%1 redirect;
rewrite ^/([^\.]+)$ /$1.html break;
}
I tried to convert apache to nginx (winginx.com) from suggestion here with no success.
This code does not work. I am trying to redirect and add trailing slash:
if (!-e $request_filename){
rewrite ^/(.*)/$ /$1 redirect;
}
if (!-e $request_filename){
rewrite ^/(.*[^/])$ /$1/ redirect;
}
Any suggestion would make me happy.
Upvotes: 2
Views: 4650
Reputation: 49682
I have tested this configuration and it appears to meet your requirements (YMMV):
root /path/to/root;
location / {
try_files $uri @rewrite;
}
location @rewrite {
rewrite ^(.*[^/])$ $1/ permanent;
rewrite ^(.*)/$ $1.html break;
}
The first location attempts to serve the file as is which is necessary for resource files. Otherwise it defers to the named location.
The named location will permanently redirect any URIs which do not end in /
. Otherwise, the .html
file is served.
See this document and this document for details.
Upvotes: 2
Reputation: 1107
I think a try_files would be appropriate here. For example:
location /about {
try_files about.html;
}
Upvotes: 0