How I can rewrite a url that contain a date on NGINX - nginx

I have a problem with a rewrite url on wordpress.
The url looks like this: example.com/blog/2020-12
I would like to turn it into: example.com/blog/2020/12
I tried this unsuccessfully:
location ~ "^/blog/([0-9]{4}+)-([0-9]{2}+)/$" {
rewrite ^(.*)-(.*) $1/$2 permanent;
}

Related

nginx rewrite rule with query parameter

My URL looks like:
localhost/video-detail?videoID=T0r-uCXvDzQ
I want to serve a page with name: T0r-uCXvDzQ.html (videoID.html) which is present in server's file system.
I am trying to write the rewrite rule as follows:-
location / {
rewrite ^/video-detail?videoID=(.*) /$1.html;
}
Also tried:
location / {
rewrite ^/video-detail?videoID=(.*) /$arg_videoID.html;
}
But they are giving 404 error.
How can I use the query parameters in the output rewrite rule.
The following worked for me:-
if ($args ~* "videoID=(.*)") {
set $key1 $1;
rewrite ^(/video-detail)$ /$key1.html;
}

Nginx rewrite URL, change parts of URL

Is it possible in Nginx to rewrite URL like this?
http://www.example.com/city/person/contact.php?id=name
to
http://www.example.com/city/person/name/contact
How about:
location ~ /city/person/(.*)/(.*) {
rewrite ^/city/person/$2.php?id=$1 last;
}

NGINX location rewrite for WP permalink

So, I have WP with almost classic permalink structure, like: /%category%/%year%/%month%/%day%/%postname%/
I would like to redirect with 301 error code to new style, like /%year%/%month%/%day%/%postname%/ or simple /%postname%/.
I've made this rule, but they return same URL.
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $1 permanent;
}
Or, for /%postname%/ I've tried code like this:
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $5 permanent;
}
But this code return empty response.
Please, help me solve the issue. I can't catch were I'm wrong. Thanks!
So, it was very simple:
location ~ "^/([a-zA-Z0-9_.-]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/([a-zA-Z0-9_.-]+)/$" {
rewrite ^(.*)/(.*)/(.*)/(.*)/(.*)/ /$5/ permanent;
}
This rule rewrite /category/year/month/day/postname/ to /postname/ URL in WP with 301 error code for search engines.

Rewrite nginx cut last location

I would like to rewrite the following URL http://example.net/bar/foo to http://example.net/bar.
Do you know how to do this ?
Use this :
location /bar {
rewrite ^/bar/(.*)/[^/]+$ /bar/$1;
}

how do I rewrite a URL in nginx to an updated location?

I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}

Resources