nginx redirect old URL to new URL - nginx

We just updated all our product url to new url. It only add some letter in the URL.
An example
old URL:
http://www.example.com/parent/children/product.html
new URL:
http://www.example.com/new-parent/children/product.html
(just added "new-" in "parent")
I try with this but not work.
location /parent {
rewrite ^/parent(.*) http://$server_name/new-parent$1 permanent;
}
So anyone can help me to correct this redirection?

Try to put rewrite ^/parent(.*) http://$server_name/new-parent$1 permanent; into server directive, not into /parent location.

Related

Remove two parts of url permanantly

I have redesigned a website and changed the url formats too. Now i need to change the old url to new one.
Here is my old url:
/solumina-G8/api/v1.0/user-avatar/avatarImage/currentUser
The new url would be:
/api/user-avatar/avatarImage/currentUser
I was successful remove just one of them using rewrite rule
rewrite ^/solumina-G8(.*)$ $1 permanent;
But I can't find how to remove both Solumina-g8 and v1.0
Try this:
rewrite ^/solumina-G8/api/v1.0/(.*)$ /api/$1 permanent;

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 to prepend something to a url

I want to rewrite all my JPG file URLs using mobify CDN. For that, all I have to do is prepend the URL
https://ir0.mobify.com/jpg50/ to my existing URL. So for example, if I have the URL
http://xxx.yyy.com/wp-content/uploads/2290/07/abc.png then the user has to be redirected to
https://ir0.mobify.com/jpg50/http://xxx.yyy.com/wp-content/uploads/2290/07/abc.jpg
I wrote the following code in my nginx config. I tested the regexs at regexlib and they seem to be fine.Still do not understand what is wrong with my config. Please help.
location ~ \.jpg$
{
rewrite ^http://(.*).jpg$ https://ir0.mobify.com/jpg50/$uri last;
}
Try this ...
location ~ \.jpg$ {
return 301 https://ir0.mobify.com/jpg50$request_uri;
}

Redirect a specific URL to another in NGINX

I need to redirect a specific url containing a parameter and an anchor to a new URL:
/pages/page.php3?page=fond_razeni_gk.htm#GKI
to
http://www.newserver.com/o-knihovne
I try:
rewrite ^/pages/page.php3\?page=fond_razeni_gk.htm$ http://www.newserver.com/o-knihovne? redirect;
but this does not work. Could you please advise me what is wrong?
try this configuration:
location / {
rewrite ^/pages/page.php3(.*)$ http://www.newserver.com/o-knihovne$1 permanent;
}

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