I recently created two folders containing index files with the respective language of the folders, pt
(Brazil) & en
(English).
When the user accesses the page, I use a control structure to check the user's browser language, if the browser language is pt, then I redirect the user to https://mywebsite.com/pt/index
, otherwise, I redirect the user to https://mywebsite.com/en/index
.
Maybe this idea that I had to translate, is not so professional, but okay, the point is that I would like to remove the path from the URL folders of the website, that is, I want it to appear https://mywebsite.com/index
regardless if I redirected the user, do you understand?
That is, if I redirect the user to https://mywebsite.com/en/index
, I don't want /pt
to be displayed, just /index
.
Is it possible to do this using Nginx?
EDIT
Tree ( /var/www)
├── mywebsite.com
│ └── html
│ ├── 404.html
│ ├── css
│ │ ├── bootstrap.css
│ │ └── style.css
│ ├── index.php
│ ├── jquery
│ │ └── jquery.js
│ ├── js
│ │ ├── bootstrap.js
│ │ └── vue.js
│ ├── logout.php
│ ├── pt
│ │ ├── index.php
│ │ ├── logout.php
│ │ ├── signin.php
│ │ └── signup.php
│ ├── signin.php
│ ├── signup.php
└── html
├── index.html
└── index.nginx-debian.html
index.php
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($lang == 'pt') {
header('Location: pt/');
return;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>English</title>
</head>
<body>
<h1>Welcome! You're browsing the website with the English language.</h1>
</body>
</html>
pt/index.php
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if(!$lang == 'pt') {
header('Location: https://mywebsite.com');
return;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Português</title>
</head>
<body>
<h1>Bem vindo! Você está navegando no website com o idioma Português.</h1>
</body>
</html>
Nginx ( /etc/nginx/sites-available/mywebsite )
server {
root /var/www/mywebsite.com/html;
try_files $uri $uri/ @extensionless-php;
index index.php;
server_name mywebsite.com www.mywebsite.com;
error_page 403 http://mywebsite.com/forbidden.html;
error_page 404 =301 http://mywebsite.com/404.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
internal;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
listen 443;
ssl on;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
access_log /var/log/wss-access-ssl.log;
error_log /var/log/wss-error-ssl.log;
}
server {
if ($host = www.mywebsite.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mywebsite.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name mywebsite.com www.mywebsite.com;
return 404; # managed by Certbot
The control structure I said is now visible in the code. Really, this is working, but I don't know if what I did is appropriate, if it is professional or not, if there are risks ...
So judging by the information now available, when the user enters the page, the script will check the language of the user's browser and if it is pt
, it will redirect to the page in Portuguese, and how that page in Portuguese is inside a folder with the name pt
, soon the name of this folder will appear in the URL
I would like to know if there is a way to hide the name of the folder?
Note:
Richard Smith said: The user needs the ability to choose/change the language. Before I did this to translate into the user's language, I used a Google Translate button that could translate the page into the language the user chose, however, I didn't think it was that cool, it was making the website slow to load, and most users in Brazil haven't even used that. So I thought about how to automatically translate the page into the user's browser language, and I did what is in the code above but I really don't know if it's the best one to do
Ivan Shatsky said: choose one of two different root folders at nginx config. I didn't quite understand what that means. Should I create another configuration file in /etc/nginx/sites-available?