Reputation: 141
I need to rewrite a slug to a querystring in nginx without changing the browser url.
In Apache the following works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/theblog/([0-9a-zA-Z-]+)$ /theblog/blog?slug=$1 [L,QSA]
In nginx I've tried both of the following:
rewrite ^\/theblog\/((?!blog?)[^\/]+)$ /theblog/blog?slug=$1 break;
rewrite ^\/theblog\/((?!blog?)[^\/]+)$ /theblog/blog?slug=$1 last;
They both REDIRECT to the querystring version of the url, which "works" but isn't what i want. I don't want to change the url in the browser.
I want the url to stay: http://example.com/theblog/test
Not redirect to: http://example.com/theblog/blog?slug=test
Even though that is the resource being requested.
The site is static html with javascript fetching the data, php is not even enabled for this domain.
This server is using: Plesk Obsidian 18.0.36 Nginx 1.20.1
Hopefully that makes sense. Any help would be greatly appreciated.
Thanks!
Upvotes: 0
Views: 769
Reputation: 15478
Both of those rewrite
directives should not lead to redirect but to internal URI rewrite. It is possible that this redirect is generated by your backend. If your backend is a PHP application, you can try to alter the REQUEST_URI
PHP-FPM parameter:
rewrite ^\/theblog\/((?!blog?)[^\/]+)$ /theblog/blog?slug=$1 last;
...
location ~ \.php$ {
...
include fastcgi_params;
# default "fastcgi_params" file contains the following line:
# fastcgi_param REQUEST_URI $request_uri;
# now we should overwrite it with a new value:
fastcgi_param REQUEST_URI $uri$is_args$args;
...
}
Read the very first part of this answer for additional details.
Upvotes: 0