nginx php friendly URL redirection without interfering with index.php causing /index

前端 未结 1 843
暗喜
暗喜 2021-01-26 23:30

I\'ve tried so many different configurations to enable permanent redirection of any request ending with .php to redirect to itself without .php.

The issue is, I can\'t g

相关标签:
1条回答
  • 2021-01-27 00:00

    You're likely experiencing the issue with your browser having your prior 301 Moved Permanently redirects in its cache, thus some of your newer code would not be having any effect.

    Clear the cache, and try something like this:

    index index.php;
    if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2;   }
    

    The above line supports stripping out not only index.php, but also just index as you might have from your earlier question, as well as just .php, yet it preserves the possible query string from $args, too.

    To avoid having support for stripping just index, still supporting the rest of the features as above, use the following instead:

    if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index)?\.php(\?.*)?$) {   return 301 /$1$2;   }
    

    P.S. The original explanation of this trick is here: nginx redirect loop, remove index.php from url.

    0 讨论(0)
提交回复
热议问题