Drop last part of URL with Nginx rewrite - nginx

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 /

Related

Nginx rewrite - swap path in URL

Please help me with nginx:
url www.server.com/products/customer/books
should be www.server.com/customer/products/books
So i need to swap /products/ with /customer/
Yes, you can do this with a rewrite. Add the following to your server block:
rewrite ^/customer/products/(.*)$ /products/customer/$1 last;
Or, if you have multiple URLs (ie customer & business), you can do this:
rewrite ^/(customer|business)/products/(.*)$ /products/$1/$2 last;

nginx URL rewrites, applying wildcard

I am using nginx to rewrite some urls. how would I take the wildcard part of the initial url and apply it to the destination url?
ex. ^/il-it/news /news where news is its the wildcard and its auto applied to the destination.
rewrite ^/il-it(/.*)?$ / permanent;
Try this -
rewrite ^/il-it/(.+)/?$ /$1 permanent;

Rewrite Rule. Remove Numbers from Url

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

Why there is one more '/' after Nginx rewrite?

In the nginx.conf of website.com I put this:
rewrite ^test/(.+)$ http://www.websitenew.com/test/$1 permanent;
Then when I use curl -I www.website.com/test/en I got this in the redirection header:
Location: http://www.websitenew.com//test/en
I can see that website.com had successfully rewritten to websitenew.com and the URL scheme is also correct, but why there is one more / between websitenew.com and /test?
Thanks,
rewrite ^test/(.+)$ http://www.websitenew.com/test/$1 permanent;
This rule can't work actually, and your redirect is made by something else. There's no URI in nginx that not start with /, but your rule doesn't have / at the beginning.

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