nginx : rewrite rule to remove /index.html from the $request_uri

前端 未结 7 596
[愿得一人]
[愿得一人] 2020-12-30 10:17

I\'ve seen a few ways to rewrite the $request_uri and add the index.html to it when that particular file exists in the file system, like so:

<
相关标签:
7条回答
  • 2020-12-30 11:03

    This is working for me:

    rewrite ^(|/(.*))/index\.html$ /$2 permanent;
    

    It covers both the root instance /index.html and lower instances /bar/index.html

    The first part of the regex basically translates as: [nothing] or /[something] - in the first case $2 is an empty string so you redirect to just /, in the second case $2 is [something] so you redirect to /[something]

    I actually went a bit fancier to cover index.html, index.htm, and index.php

    rewrite ^(|/(.*))/index\.(html?|php)$ /$2 permanent;
    
    0 讨论(0)
提交回复
热议问题