NGINX -Rewrite url keeping part of url - nginx

Please help me to create nginx redirect like below:
https://mypage.com?via=181696076 -> https://newpage.com/checkout/?cc=GS950N&short_id=gi&via=181696076
The parameter(181696076) will change, will be only a number and will not have a fixed length
Can I do this using location?
rewrite ^/(.*)$ https://newpage.com/checkout/?cc=GS950N&short_id=gi&$1;
I tried few combination, but it does not work for me - like the rewriting on server level
Thanks

I found such solution for this problem:
location ~ ^/(.*)$ {
return 301 https://newpage.com/checkout/?cc=GK960N&short_id=gi&via=$1;
}

Related

Rewrite Nginx Url only on specific path

So I have a main path https://app.example.com and I want it to keep it that, way but on all https://app.expample.com/p/* paths I want it to be rewritten to a short version of the url like. How can I achieve this?
I tried
location = /p/(.*)$ {
rewrite ^ https://example.com/p/$request_uri permanent; and also return 301 https://example.com/p/$request_uri;
}
but this resulted in too many http redirects and an error

Redirecting old url to Nginx with param

I have an old URL structure like this archives.php?l=e and I moved it to a new structure archives/e
I'm trying to 301 redirect all the old urls, but I have run into troubles. This is what I'm currently doing but it's not working.
location ^~ /list.php {
rewrite /archives/$1/ permanent;
}
Any help would be greatly appreciated.
I figured it out and I have to say Nginx has some nice magic for this. Using arg_l solved it. Somehow it creates a variable for each param.
location ^~ /archives.php {
rewrite ^/archives.php$ /archives/$arg_l? permanent;
}

Nginx rewrite rule: Add subfolder to URI if not there

Hello I'm trying to write a Nginx rewrite rule that adds a subdirectory to my URL if it's missing. For example, I need http://example.com to be re-written (and redirected) to http://example.com/legacy-app. Can't seem to find the proper example to do this.
You can use a rewrite directive, but an exact match location block is most efficient:
location = / {
return 301 /legacy-app;
}
See this document for more.
We ended up using this:
rewrite ^/$ /legacy-app/ last;

Rewrite a URL in nginx without redirecting

I'm trying to create a rewrite with nginx that will change the displayed URL without actually redirecting to it. EG: http://example.com/item/rest-of-path would become http://example.com/folder/rest-of-path. I've been working with different variations of this code with in my nginx.conf:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 last;
}
but that doesn't seem to be doing the trick. Any ideas where I'm going wrong here? I'll admit I'm still pretty new to server-side rewrites in general.
Try this:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 break;
}
use break.
Try this. It isn't necessary to place it under the location block, and don't forget to reload nginx.
rewrite ^/example/(.+)$ /folder/$1 last;

Rewrite old image url with nginx

I have a single image that I renamed and need to redirect from the old image url to the new one my server uses nginx, I'm not having much success with the following rewrite:
rewrite ^/assets/avatar/avatar.png /assets/avatar/newavatar.png permanent;
Does someone know what is wrong?
If you want to use rewrite you need to use a full path to the new location
rewrite ^/assets/avatar/avatar.png http://example.com/assets/avatar/newavatar.png permanent;
A better way though is using a return
location /assets/avatar/avatar.png {
return 301 $scheme://example.com/assets/avatar/newavatar.png;
}
If you want to just rewrite, you need to change permanent to last or break

Resources