Rewrite with 404 urls with nginx - regex

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;
}

Related

nginx match a folder in path and with no .html which needs to rewrite with .html appended

I have a URL which contains subfolders in the path and may or may not contain .html.
e.g.
https://www.example.com/store/test/en/test/ANYCATEGORY/ANDCATEGORY/OPTIONALCATEGORY/PROGRAMNAME/p/100002020
I want to match if the url contains "/p/" in the path AND there is no .html at the end.
If these conditions are met, I want to append with .html
e.g.
https://www.example.com/store/test/en/test/ANYCATEGORY/ANDCATEGORY/OPTIONALCATEGORY/PROGRAMNAME/p/100002020
I have tried with if statements and location but no luck with any of those.
This is what I have tried with no luck:
if ($uri ~ ^(.*)/p/(.*)$){
rewrite ^(.+)/$ $1.html permanent;
}
if ($uri ~ ^(.*)/p/(.*[^.html])$){
rewrite ^(.+)/$ $1.html permanent;
}
location ~ ^(.*)/p/(.*[^.html])$ {
rewrite ^(.+)/$ $1.html permanent;
}
You need a positive look ahead (?=.*\/p\/) for ensuring the presence of /p/ and a negative look ahead (?!.*\.html$) to ensure the string doesn't end with .html. The correct regex to use is this,
^(?=.*\/p\/)(?!.*\.html$).+$
Demo
Then try rewriting your if condition the way you were doing, which should be this,
location ~ ^(?=.*\/p\/)(?!.*\.html$).+$ {
rewrite ^(.+)/$ $1.html permanent;
}
Also, in your regex [^.html] this is a character negation and will only negate certain characters present in character set after ^ and for negating a sequence of characters, you need to use a negative look ahead pattern like I did in my regex.
location ~ ^(?=.*\/p\/)(?!.*\.html$).+$ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(.+)$ $1.html permanent;
}

Passing a capture group in query parameters in NGINX URI

In NGINX, is there any way to pass a capture group in a query parameter for a rewrite outside of a location block, such as this? Thanks!
if ($request_uri = /abc/def?query=(.*) {
rewrite ^.*$ /123/$1? permanent;
}
Below should work for you
if ($request_uri ~ "/abc/def\?query=") {
rewrite ^.*$ /123/$arg_query/? 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;

Combine three nginx rewrites into one

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

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 ?