nginx redirect loop, remove index.php from url

前端 未结 5 1356
长情又很酷
长情又很酷 2020-11-29 03:25

I want any requests like http://example.com/whatever/index.php, to do a 301 redirect to http://example.com/whatever/.

I tried adding:

5条回答
  •  再見小時候
    2020-11-29 04:06

    Great question, with the solution similar to another one I've answered on ServerFault recently, although it's much simpler here, and you know exactly what you need.

    What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.php script, as defined through the index directive.

    This should do just that, avoiding the loops:

    server {
        index index.php;
    
        if ($request_uri ~* "^(.*/)index\.php$") {
            return 301 $1;
        }
    
        location / {
    
            # ...
        }
    }
    

提交回复
热议问题