
Nginx의 URL 문자열에서 모든 .html 확장자와 index.html 항목을 어떻게 제거합니까?
http://www.mysite/index.html
에 http://www.mysite
http://www.mysite/articles/index.html
에 http://www.mysite/articles
http://www.mysite/contact.html
에http://www.mysite/contact
http://www.mysite/foo/bar/index.html
http://www.mysite/foo/bar
편집하다:내 conf 파일은 다음과 같습니다.
서버 {
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;
}
}
답변1
재작성(클라이언트에 표시된 URL을 변경하지 않고 제거된 URL을 파일 시스템/백엔드에 전달):
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1;
대안으로301 리디렉션을 수행할 수 있습니다(클라이언트가 새 요청을 함).
rewrite ^(.*/)index\.html$ $scheme://$host$1 permanent;
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
답변2
허용된 답변이 나에게 적합하지 않은 것 같습니다. 저는 전환을 시도하는 아파치 사용자이므로 모든 상황에서 100% 작동하지 않을 수도 있지만 제 사이트에서는 작동하는 것 같았습니다(정적 HTML 페이지, 테스트일 뿐임).
index index.html;
error_page 404 404.html;
rewrite ^(/.+)\.html$ $1;
try_files $uri.html $uri/ =404;
이것이 이런 일을 일으키고 있습니다:
- url => 액세스 중인 파일
- domain.com/ => index.html
- domain.com/somepage => somepage.html
- domain.com/arandompage => 404.html
이것이 다른 혼란스러운 이전 아파치주의자에게 도움이 되기를 바랍니다.