Combine three nginx rewrites into one - regex

Is it possible to rewrite this regular expression so it fits all my use-cases below?
location ~ \.mp3$ {
rewrite "^/(\d{4})/0(\d)/0(\d)/(.*).mp3$" /$1/$2/$3/$4.mp3 permanent;
rewrite "^/(\d{4})/0(\d)/(\d{1,2})/(.*).mp3$" /$1/$2/$3/$4.mp3 permanent;
rewrite "^/(\d{4})/(\d{1,2})/0(\d)/(.*).mp3$" /$1/$2/$3/$4.mp3 permanent;
}
Real URL: 2012/7/8/piper.mp3
Should match:
2012/7/8/piper.mp3
2012/07/8/piper.mp3
2012/7/08/piper.mp3
2012/07/08/piper.mp3
Thanks

Related

What's the best way to rewrite a single character in NGINX?

I had a bunch of urls like so:
http://domain.com/l/key-a=value-a/key-b=value-b.html
They were indexed by Google, but have since changed in our system from = signs to - signs, so I would like to redirect request like the one above to:
http://domain.com/l/key-a-value-a/key-b-value-b.html
Note: there could be one or more key-value param sets, the above is just an example.
What's the best way to do this in NGINX?
I was able to solve this using the following block:
location ~ /l/(.*)=(.*) {
rewrite ^([^=]*)=(.*)$ $scheme://$host$1-$2;
return 301;
}
This seemed to work even when there are multiple key-value pair segments in the the path, ex:
domain.com/l/color=red/size=large/shape=round
UPDATE:
I discovered that in cases where multiple key-value segments were present, there were multiple 301 redirects, and Google doesn't like this.
So I ended up going with:
location ~ /l/(.*)=(.*) {
rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7-$8-$9 permanent;
rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7-$8 permanent;
rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7 permanent;
rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6 permanent;
rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5 permanent;
rewrite ^([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4 permanent;
rewrite ^([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3 permanent;
rewrite ^([^=]*)=(.*)$ $scheme://$host$1-$2 permanent;
}
The decreasing powers of 2s example (How to replace underscore to dash with Nginx) did not work for me.

Rewrite with 404 urls with nginx

I'm trying to rewrite some urls which are showing up as 404's but the I can't get the rewrite to work. The Urls look like this /ossobuco-alla-milanese​​/1451114854360.1451114854360?time=1451114851111. I would like to remove 1451114854360.1451114854360?time=1451114851111 with a rewrite.
In my nginx config I have the following rewrite rule
rewrite "^\/(.*)\/(\d{13}\.\d{13}\?time=\d{13})$" /$1/ permanent;
I tested the regex in 2 online regex tools regex101 and regex pal and it should work but the don't seem to work on my server.
To match query string use $args:
location / {
if ($args ~* "^time=\d+") {
set $args '';
rewrite "^/(.+)/\d+\.\d+/?$" /$1 permanent;
}
}
PS: If you want to match only 13digits.13digits then use:
rewrite "^/(.+)/\d{13}\.\d{13}/?$" /$1 permanent;
In the end its was just a really simple location block and the ? removes the args.
location ~* ^/(.+)/\d+\.\d+$ {
rewrite ^/(.+)/\d+\.\d+$ /$1? permanent;
}

Nginx rewrite rule with special character and Regex

We have URLs that contains #! in their structure. I would like them to be redirected.
From:
/#!/about/
/#!/about/example/
To:
/about/
/about/example/
I tried this rule, but it didn't work:
rewrite ^([\/][#][!][\/])(.+[\/])(.+[\/])$ /$2$3 permanent;
Try this much simpler expression:
rewrite ^/\#!/(.*)$ /$1 permanent;

Nginx: Remove duplicate slashes

Basically, I want http://www.example.com//index being rewrited as http://www.example.com/index
I already found a solution for this :
if ($request_uri ~ "^[^?]*//") {
rewrite "(.*)" $scheme://$host$1 permanent;
}
But I'm worried about the efficiency of the if statement.
I believe there is a better way to do it but I couldn't find it. I'd try this with no success :
rewrite ^/(.*)//(.*)$ $1/$2 permanent;
rewrite ^/(.*)\/\/(.*)$ $1/$2 permanent;
Any idea ?

nginx Rewrite Rules to Re-write Every Match Of a Certain Regex Except

I'm writing rewrite rules in nginx.
I want to rewrite every URI that matches /A/B[anything] to /X/ except /A/B/C[/].
How do I do this?
What I've tried:
if ($request_uri ~ ^/A/B/C/?) {
break;
}
rewrite ^/A/B /X/ permanent;
This rewrites /A/B to /X/, but /A/B[anything] doesn't get redirected, nor does /A/B/C/ stay at /A/B/C/.
rewrite ^/A/B/?$ /X/ permanent;
Should do it
I figured out the issue.
I needed to make the rewrite line
rewrite ^/A/B(.*)? /X/ permanent;
The (.*)? means " and optionally match anything any amount of times."