问题原文
原帖链接 · Rumplin · CC BY-SA 4.0
I'm trying to setup a Nginx configuration for nice URLs without .html extension.
What I currently have works, but the / URI redirects to /index and I don't want that.
The URL rewrite rules should be as follows:
/index.html -> /
/test.html -> /test
/subpage/ -> /subpage
/subpage/test.html -> /subpage/test
/sub/sub/index.html -> /sub/sub
/sub/sub/ -> /sub/sub
/somepage/ -> /somepage
/somepage.html -> /somepage
Here is the configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
set $root /app/www/public;
if (!-d /app/www/public) {
set $root /config/www;
}
root $root;
index index.html;
error_page 401 /401;
error_page 403 /403;
error_page 404 /404;
error_page 500 502 503 504 /500;
location / {
#redirect index.html to /
if ( $request_uri ~ "index.html" ) {
rewrite ^(.*)/ $1/ permanent;
}
#remove .html from request
rewrite ^/(.*)\.html$ /$1 permanent;
#remove the trailing /
rewrite ^/(.*)/$ /$1 permanent;
try_files $uri.html $uri/ $uri =404;
}
location ~* \.(jpg|jpeg|png|gif|ico)$ {
expires 30d;
}
location ~* \.(css|js)$ {
expires 7d;
}
# deny access to .htaccess/.htpasswd files
location ~ /\.ht {
deny all;
}
}
NOTE: this Nginx is running in a docker container.
已采纳解答
原帖链接 · Stephen Ostermiller · CC BY-SA 4.0
I got it to work with this configuration which I tested on nginx/1.18.0 (Ubuntu):
index XXXXXX.DISABLE.INDEX.DOCUMENT.XXXXXX;
location / {
#remove index.html from request
rewrite ^/(.*)/index(\.html)?$ /$1 permanent;
rewrite ^/(.*)index(\.html)?$ /$1 permanent;
#remove .html from request
rewrite ^/(.*)\.html$ /$1 permanent;
#remove the trailing /
rewrite ^/(.*)/$ /$1 permanent;
try_files $uri ${uri}/index.html ${uri}.html =404;
}
By disabling the index functionality, you can handle add index.html to the try_files which is more compatible with the rewrite rules.
You can control if /sub/index.html or /sub.html takes precedence for a request for /sub by changing the order of ${uri}/index.html and ${uri}.html in try_files.
Tests:
curl --head http://localhost/->200 OK(powered by/index.html)curl --head http://localhost/index->301 Moved PermanentlytoLocation: http://localhost/curl --head http://localhost/index.html->301 Moved PermanentlytoLocation: http://localhost/curl --head http://localhost/page->200 OK(powered by/page.html)curl --head http://localhost/page.html->301 Moved PermanentlytoLocation: http://localhost/pagecurl --head http://localhost/sub->200 OK(powered by/sub/index.html)curl --head http://localhost/sub/->301 Moved PermanentlytoLocation: http://localhost/subcurl --head http://localhost/sub/->301 Moved PermanentlytoLocation: http://localhost/subcurl --head http://localhost/sub/index->301 Moved PermanentlytoLocation: http://localhost/subcurl --head http://localhost/sub/index.html->301 Moved PermanentlytoLocation: http://localhost/sub
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
