Hello, i have this url xxxxx.com/xxxx/xx-xx-2/xxxx and want remove the “-2” from second parameter in the url and that it stays that way xxxxx.com/xxxx/xx-xx/xxxx, The “-2” is random. i don’t have idea how to do it :( any help would appreciate it.
My apologies for my bad english
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This can be accomplished using an Nginx rewrite rule. These use regular expressions to match different parts of the URL and then “rewite” them.
There will be differences based on whether xxxx is alaways xxxx or if you also want yyyy to match, but here is a minimal working example as a reference point:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name xxxxx.com;
root /var/www/html/;
index index.html index.htm;
rewrite (xxxx/.*)-\d+(.*)$ /$1$2 last;
}
Breaking down the regex a bit:
(xxxx/.*) captures every thing highlighted in red: xxxx/xx-xx-2/xxxx-\d+ matches on: xxxx/xx-xx-2/xxxx(.*)$ captures every thing that follows: xxxx/xx-xx-2/xxxxThen /$1$2 takes the first captured part ($1) and appends the second ($2), leaving you with: xxxx/xx-xx/xxxx
regex101.com is a great tool for testing and explaining how different regular expressions work. Check out this one, and modify it as needed for your real use case:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.