Rewrite Rule. Remove Numbers from Url - nginx

Im struggling with the following nginx rewrite
I have the following url:
http://www.website.com.au/product-name-anything-13087.html
I need to remove the numbers from the end so the new url looks like
here is my failed attempt at this in nginx.
http://www.website.com.au/product-name-anything.html
location ~* ^/(.+)-([0-9]+)\.html$ {
rewrite ^(.+)$ /$1 redirect;
}
any help would be appreciated

Related

NGINX Rewrite is encoding querystring as a path

I want to rewrite internally several locations /customers/foo?bar=2 to an existing location in my nginx configuration at /blah so that it's as if the request was to /blah/customers/foo?bar=2.
location /blah {
# View in fiddler
proxy_pass http://127.0.0.1:8888/;
# Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...
Nginx is rewriting the URL with the querystring encoded as a path /blah/customers/foo%34bar=2.
The same thing happens with rewrite ^ /blah$request_uri;. It encodes the ? as %3F effectively garbling the URL.
If I do a client redirect rewrite ^ /blah$request_uri permanent; the URL is correct and contains the ? but I want an internal redirect inside my NGINX config.
Don't use $is_args$args, because the rewrite directive will automatically append any existing query string.
For example:
rewrite ^/customers/(.*) /blah/customers/$1 last;
Although, I would prefer:
rewrite ^(.*)$ /blah$1 last;
Or even:
rewrite ^ /blah$uri last;

nginx rewrite url to remove subdirectory

I would like to rewrite an URL to remove recurring parts from the URL by nginx. Is that possible?
example: dummy.com/sub1/sub2/myproject/trunk --> dummy.com/myproject/trunk
Is a nginx rewrite rule the right way to solve this problem?
location /sub1/sub2 {
rewrite ^/sub1/sub2/(.*)$ $1 last;
}
Solution was to put a proxy_pass in location /
servername dummy.com
location / {
proxy_pass 123.456.789.0:81/sub1/sub2/;
}
A rewrite was not necessary

nginx location rewrite not matching

In our nginx configuration we have defined the following rewrite to forward requests to another domain:
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
It matches URLs like http://www.example.com/service/test123 but the following URL does not match:
https://www.example.com/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Yes there is a second URL after /ext_css/ and we now that's not correct but at the moment there is no way to change that.
Is there a way to completely forward the whole path to the new server?
/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Doing it outside of the location block did the trick.
Instead of
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
we now have just
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
Now it matches everything written after /service/

Drop last part of URL with Nginx rewrite

If I have the following URL:
https://kl-office.com/directory
How can I configure Nginx to rewrite the above URL as:
https://kl-office.com
You could try this:
rewrite ^.*$ / permanent;
Although, this will rewrite any request to /

Nginx rewrite domain and URLs

I'm configuring nginx with multiple server names, and trying to set up the following rewrite rules
redirect / on old.domain.com to new.domain.com/specific_page.php
redirect old.domain.com/$1 to new.domain.com/$1
In my "server" configuration, I have already the first rewrite condition working, but I cannot find the way to write the second.
if ($host = 'old.domain.com' ) {
rewrite ^/ http://new.domain.com/my-specific/link/list/info.php permanent;
rewrite ^/(.*)$ http://old.domain.com/$request_uri? permanent;
}
Any ideas how to handle easily this scenario? (I realise this might be an unusual setup.)
Actually I managed to solve my problem :
if ($host = 'old.domain.com' ) {
rewrite ^/$ http://new.domain.com/my-specific/link/list/info permanent;
rewrite ^(.*)$ http://old.domain.com$request_uri? permanent;
}
the first rewrite rule ^/$ matches only http://old.domain.com/ and rewrites it to the requested URL
The second rewrite rule ^(.*)$ matches whatever is behind the http://old.domain.com/ and rewrites the domain only.

Resources