Nginx Config Location Regex With Language Code In Url

南楼画角 提交于 2019-12-11 00:45:32

问题


Trying to achieve constant language code in url's 1st segment with nginx regex location configuration and could not find the correct syntax.

Necessary result:

  • example.com stays example.com
  • example.com/en stays example.com/en
  • example.com/en/ stays example.com/en or example.com/en/ (don't care)
  • example.com/en/etc stays example.com/en/etc

  • example.com/etc changes to example.com/en/etc
  • example.com/etc/segment changes to example.com/en/etc/segment



    Currently I have found out this code but it still stuck in somewhere. It makes permanent loop and doesn't not use $1 argument.

    location ~ "^/(?![a-z]{2}/)(.+)$" {
        rewrite / /en/$1 permanent;
    }
    
    #Using handler here for removing index.php in uri (example.com/index.php -> example.com);
    location / {
        index index.htm index.html index.php;
        try_files $uri $uri/ @handler;
    }
    
    location @handler {
        rewrite / /index.php?$query_string;
    }
    



UPDATE: Answer can be found in this question: https://stackoverflow.com/a/33913261/2662849

来源:https://stackoverflow.com/questions/33891782/nginx-config-location-regex-with-language-code-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!