nginx从URL隐藏symfony语言环境 [英] nginx hide symfony locale from url

查看:75
本文介绍了nginx从URL隐藏symfony语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Nginx服务器中的URL重写有问题.我想从我的网址中隐藏symfony的语言环境,因此www.example.com/fr/route应该是www.example.com/route 我的nginx配置文件具有以下内容:

i have an issue with url rewriting in nginx server. i want to hide symfony's locale from my url so www.example.com/fr/route should be www.example.com/route my nginx config file has the following :

location / {
    # try to serve file directly, fallback to app.php
    try_files $uri /app.php$is_args$args;
}

# PROD
location ~ ^/app\.php(/|$) {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    internal;
}

请帮助我,非常感谢.

我已经尝试使用.htaccess,但是nginx不支持.htaccess文件

i already try with .htaccess but nginx doesn't support .htaccess files

推荐答案

您好,欢迎来到论坛.

当您用url重写标记问题时,我假设您要实现url重写而不是其他东西.也就是说,URL重写不是隐藏" URL的那部分,而是重写URL并删除了那部分-意味着在您的symfony项目中将无法使用此信息.

As you have tagged your question with url-rewriting, I am assuming that you want to achieve url rewriting instead of something else. i.e. url rewriting isn't "hiding" that part of the url, it is rewriting the url and removing that part - meaning that this information won't be available to you in your symfony project.

以下nginx规则将执行url重写:

The following nginx rule will do the url-rewriting:

rewrite ^/fr/(.*)$ /$1 permanent;    

请注意,通常在测试/创建以下规则时使用非永久选项可能是个好主意:

please note that it might be a good idea in general to use non-permanent option when testing/creating these rules like this:

rewrite ^/fr/(.*)$ /$1 redirect; 


更新了有关如何隐藏"语言环境的信息

这是您必须在symfony项目中要做的事情,而不是使用nginx.不过作为警告,symfony文档明确建议不要使用这种语言环境隐藏(请参见

This is something that you would have to do inside your symfony project instead of using nginx. As a warning though, symfony documentation explicitly advises against using this kind of locale hiding (see The locale and the url). But if you for some reason still want to do this kind of locale hiding, I would assume one approach would be to create generic catcher routes for handling your locales - something like this:

locale_redirect:
    path:  /{_locale}/{params}
    controller: App\Controller\LocaleRedirectController::locale
    requirements:
        _locale: en|fr|de
        params: '.+'

在您的控制器中,如下所示:

And in you controller something like this:

public function localeAction(Request $request, $locale, $params)
{
    // set the locale sticky
    // https://symfony.com/doc/current/session/locale_sticky_session.html

    // redirect to the url without the locale
    return new RedirectResponse("/$params");
}

我实际上没有尝试过此代码/逻辑-并且不会这样做,因为symfony文档建议(出于正当理由)不建议这样做-因此,此示例代码/此逻辑可能存在一些小问题,您需要如果您决定采用这种方式,请自己弄清楚.

I haven't actually tried this code / logic - and wouldn't do this because symfony documentation advises against it (for valid reasons) - so there might be some small issues with this example code / this logic that you need to figure out for yourself if you decide to go this way.

这篇关于nginx从URL隐藏symfony语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆