URL Rewrites in Nginx - 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;
}

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

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;

proxy_pass in nginx to publish webapp under a different directory

I have this location element:
location ~* ^/publicapp {
proxy_pass https://myserver.domain.local;
}
The server myserver.domain.local hosts a web application located under /myapp.
I want to make it publicly available via https://www.mywebsite.com/publicapp. How do I tell nginx to translate /myapp to /publicapp?
Please keep in mind that I use ~* to allow case-insensitivity. Thus, I cannot use a URI with proxy_pass.
Kind regards,
Kevin
Try this:
location ~* /publicapp/ {
rewrite ^/publicapp/(.*)$ /myapp/$1 break;
proxy_pass https://myserver.domain.local;
}
This will rewrite your path and use new one at the .local server.
It works using
rewrite ^/publicapp/(.*) /myapp/$1 break;
At least it does with my very simple application.
Now I have to figure out how to do proper link translation (sorry for using ISA Server/TMG terms, don't know if it's the same in nginx).
Thanks to pythagor :-)
edit:
Works only if I keep a trailing slash after the url in the browser (https://www.mywebsite.com/publicapp/).
another edit:
To make sure URLs end with a slash:
rewrite ^([^.]*[^/])$ $1/ permanent;
Taken from: here (first answer)

nginx rewrite question

I'm new to nginx and I got a rewrite problem here:
I want to permanently redirect http://domain1.com/abc.php to http://domain2.com,
but I want to keep http://domain1.com/abc.php?param=value, I've tried put
rewrite ^/abc\.php$ http://domain2.com last;
which works for http://domain1.com/abc.php, unfortunately it rewrites everything that starts with the '/abc.php', I'm really confused why this is happening, any ideas?
Thanks in advance.
Nginx rewrites generally don't "see" the query string as part of the URI, which is why your existing rewrite isn't working - to Nginx it's always ^/abc\.php$ whether there's a query string or not.
Instead, I'd try this (adapted from the documentation):
if ($args !~ param=value) {
rewrite ^/abc\.php$ http://domain2.com permanent;
}
But be aware that if is evil.

Resources