I'm moving a project to use Docker with Nginx, which previously used Apache. It's a simple PHP project, but I can't get the rewriting to work like it did on Apache. The old .htaccess
is simple;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
It redirects all page requests to the index.php
file with a page
parameter, which is then parsed in index.php
with a custom PageController
. I can't however get this to work with Nginx rewrites and it's confusing me a lot.
The goal is to rewrite URLs like index.php?page=forgot_password
to forgot_password
.
My current nginx.conf is
server {
listen 80 default_server;
root /app/app;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
which obviously doesn't work since there's no rewrite rules.
I've tried a few other configurations, for example the Nginx configuration in the Symfony documentation, which redirects all pages to index.php
but doesn't provide the page
query parameter.
I've tried other configurations like
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?page=$1 break;
}
}
which causes Firefox to prompt me to download the forgot_password file.
I've had other attempts (which I can't remember sadly) that did seem to work the way I wanted, but caused JS, CSS and other resources to no longer work.
How can I get this working? I feel like I'm missing something simple.