
Como eu removeria todas as extensões .html, bem como quaisquer ocorrências de index.html de uma string de URL no Nginx
http://www.mysite/index.html
para http://www.mysite
http://www.mysite/articles/index.html
para http://www.mysite/articles
http://www.mysite/contact.html
para http://www.mysite/contact
http://www.mysite/foo/bar/index.html
parahttp://www.mysite/foo/bar
EDITAR:Aqui está meu arquivo conf:
servidor {
listen 80;
server_name staging.mysite.com;
root /var/www/staging.mysite.com;
index index.html index.htm;
access_log /var/log/nginx/staging.mysite.com.log spiegle;
#error_page 404 /404.html;
#error_page 500 503 /500.html;
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1;
rewrite ^(.*/)index\.html$ $scheme://$host$1 permanent;
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
rewrite ^/about-us /about permanent
rewrite ^/contact-us /contact permanent;
try_files $uri.html $uri/ /index.html;
}
}
Responder1
Como uma reescrita (passar o URL removido para o sistema de arquivos/backend sem alterar o URL mostrado ao cliente):
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1;
alternativamentevocê pode fazer um redirecionamento 301 (o cliente faz uma nova solicitação):
rewrite ^(.*/)index\.html$ $scheme://$host$1 permanent;
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
Responder2
A resposta aceita não pareceu funcionar para mim. Eu sou um cara apache que está fazendo a mudança, então isso pode não funcionar 100% em todas as circunstâncias, mas pareceu funcionar no meu site (páginas HTML estáticas, apenas um teste):
index index.html;
error_page 404 404.html;
rewrite ^(/.+)\.html$ $1;
try_files $uri.html $uri/ =404;
Isso está fazendo isso acontecer:
- url => arquivo que está acessando
- domínio.com/ => index.html
- domínio.com/somepage => somepage.html
- domínio.com/arandompage => 404.html
Esperançosamente, isso ajudará outros ex-apacheistas confusos.