I'm trying to get Nginx to rewrite a fake directory to a query string to be used as the language. For example:
/fr/example.php
should look like that in the URL, but should rewrite to
/example.php?language=fr
Here is the code I have tried in my Nginx config:
rewrite "^(/(fr|en))?/(.*).php$" /$3.php?language=$2 last;
When I visit example.php
, I get the correct page, but when I visit /fr/example.php
, I get a 404 error.
Edit: This seems to be somehow related to the fact that it is a .php
extension. If I try with .html instead, it works. But unfortunately I need it to be php. Here is a little more of my nginx.conf
that might be helpful, found just a little later than the code I posted above:
location / {
root /var/www/example.com;
index index.php index.html index.htm;
rewrite "^(/(fr|en))?/(.*).php$" /$3.php?language=$2 last;
}
error_page 404 /404.php;
location /404.php {
root /var/www/example.com;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# CUSTOM
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
# END CUSTOM
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}