2021-05-29

Nginx URL rewrite for language parameters

I have recently localized my php webpage with Gettext in a VPS running LEMP Debian 10 (PHP 7.3.27-1~deb10u1). The localized versions of the pages have the following URLs:

example.com/index.php?lang=es_ES

and

example.com/index.php?lang=en_GB

and so on.

I am trying get Nginx to rewrite the URL requests to fake folders in the following way:

example.com/en/index.php to=> example.com/index.php?lang=en_GB

example.com/es/index.php to=> example.com/index.php?lang=es_ES

and

example.com/en/manage/index.php to=> example.com/manage/index.php?lang=en_EN

example.com/es/manage/index.php to=> example.com/manage/index.php?lang=es_ES

And so on. I have tried other solutions with no success. My Nginx server block configuration is as follows:

server {

    root /var/www/example.com;
    index index.html index.htm index.php;

    server_name example.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    location ~ /(.*)$ {
        try_files $uri $uri/ /profiles.php?user-id=$1; #pretty urls for user profiles
    }

    listen 443 ssl;
}

There is already a rewrite in place: example.com/123 => example.com/profiles.php?user-id=123. I'd appreciate some insight on making the desired rewrites to work, without affecting the one already in place. Thanks



from Recent Questions - Stack Overflow https://ift.tt/3uvRj2E
https://ift.tt/eA8V8J

No comments:

Post a Comment