Rewriting requests to the first URL structure (with or without subdirectory) doesn't require a .htaccess file in Nginx. Configure the rewriting in your site config like this:
index index.php index.html index.htm;
location /qa {
try_files $uri @question2answer;
}
location @question2answer {
if (!-e $request_filename) {
rewrite ^/qa/(.+)?$ /qa/index.php?qa-rewrite=$1 last;
}
}
location ~* "^/qa/index\.php(/|$)" {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
The fastCGI section is just included for completeness, it's not related to the URL structure rewriting.
Notes:
- Embedding the rewrite rule in the if statement ensures that URL structures 3 and 4 keep working.
- Replace /qa with the correct subdirectory for your installation.
- Replace the path to the FPM socket with the one appropriate for the PHP/FPM version you're using.
If your Q2A installation isn't in a subdirectory, simply omit the subdirectory from the config:
index index.php index.html index.htm;
location / {
try_files $uri @question2answer;
}
location @question2answer {
if (!-e $request_filename) {
rewrite ^/(.+)?$ /index.php?qa-rewrite=$1 last;
}
}