I am struggling with some Nginx rewrits for url arguments passed to index.php:
- Old:
?topic=10126.msg36887
- New:
?posts/36887/
My two questions:
1) How do I rewrite to the new argument structure since it uses "parameter/value/" rather than the traditional "parameter=value" structure?
2) My "if" statement isn't triggering, and I can't figure out why...
The test url domain.com/forum/index.php?topic=10126.msg36887
should redirect to /success
, but it isn't rewritten at all.
Here's my current Nginx config:
location /forum/ {
index index.php index.html index.htm;
try_files $uri $uri/ /forum/index.php?$uri&$args;
location /forum/index.php {
# I know Nginx 'If is Evil', but it's the only way
# to trigger rewrites on url parameters
if ($arg_topic ~ [0-9]+\.\bmsg([0-9]+) {
# testing whether 'if' triggers:
rewrite ^ /success? redirect;
# full rewrite:
# rewrite ^\/forum\/index\.php ^\/forum\/index\.php\?posts\/$1\/? redirect;
}
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Putting this in the
server
config block should work: the regexp should match what you need correctly and by stashing the required ID in a variable you can use it later in the rewrite.Assuming that you won't have a 'topic' argument in other parts of the site, there's really no need to scope this in a location - even if you do, you can alter the first part of the rewrite to match /forum/index.php only.
If it's only about 'topic' argument this might work:
On 'http' level of your nginx.conf declare following 'map':
On 'server' level add appropriate 'location':
Ensure that you have specified 'root' directive on server level so that 'SCRIPT_FILENAME' filled with correct value.
Also you may check whether '$topic_id' is zero (eg. topic_id= is missing or have incorrect value).
Using this your '$_GET' array will contain something like: