Rewrite a URL in nginx without redirecting - nginx

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;

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

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;

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;
}

URL Rewrites in Nginx

With Nginx how would I rewrite
http://www.xxxx.com.au/forum/7-forums/
So it displays as
http://www.xxxx.com.au/forum/
I haven't had much experience with this and I am not even sure if it is possible so any help is much appreciated, thanks.
Simple rewrite could be something like
rewrite /forum/7-forums/(.*) /forum/$1 last;
But then if I assume that /forum will always be handled by /7-forums I can use alias with location
location /forum {
alias /forum/7-forums;
}

Resources