0

I need to remove /web/guest/ from all urls on a portal via nginx.

Currently urls looks like this:

www.mywebsite.com/en/web/guest/blog-information
www.mywebsite.com/en/web/guest/something-else/information2
www.mywebsite.com/en/web/guest/blog-information3
and so on....

Should be:

www.mywebsite.com/en/blog-information
www.mywebsite.com/en/something-else/information2
www.mywebsite.com/en/blog-information3
and so on....

What should I add in nginx.conf in order to make this change work ?

RMP
  • 15
  • 1
  • 6
  • And your question is? What have you done so far? – Eiko Jul 11 '16 at 09:48
  • My question is how to change urls. I found this info on nginx portal but dont know how to put everything in correct way in order to make it work: server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403; ... } – RMP Jul 11 '16 at 09:51

1 Answers1

0

This can be done with nginx rewrite, try add a rule like this:

rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent;

This will remove the last /web/guest/ in your uri, you can write a more specific rewrite rule depending on your situation.

The last parameter given above is an optional flag, permanent is for 301 redirection and redirect is for 302, there are also other options so you'd better read the docs for more detailed information.

Hammer
  • 1,514
  • 2
  • 14
  • 22
  • Shoud I insert in server or location context ? – RMP Jul 11 '16 at 12:22
  • @RMP It depends on how you want to do the direct, if you would like a total direct, like redirect all urls containing /web/guest/, you can add it inside the server block. if you only want to do it for a certain location, then put it inside a location block. – Hammer Jul 11 '16 at 15:15
  • I have added `server { rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent; }` . All urls still have /web/guest/. – RMP Jul 12 '16 at 14:00
  • @RMP did you add the line ``rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent;`` into your existing server block or you just added a new server block? – Hammer Jul 12 '16 at 14:02
  • @RMP paste your full server block and let's find out together/ – Hammer Jul 13 '16 at 06:51
  • `server { listen 80; server_name my.domain.com; rewrite ^ https://my.domain.com permanent; rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent; }` – RMP Jul 13 '16 at 10:16
  • @RMP what is this rewrite rule for ``rewrite ^ https://my.domain.com permanent;``? – Hammer Jul 13 '16 at 15:49
  • @RMP Yes, the rule will throw a 301 redirect to https and the second rewrite rule will NOT work b because the redirected request will be handled in a server block listening to port 443, not this one. – Hammer Jul 14 '16 at 06:45
  • Do you have any suggestions in this case ? – RMP Jul 14 '16 at 06:56
  • You mean like this ? `server { listen 80; server_name my.domain.com; rewrite ^ https://my.domain.com permanent; rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent; }` Maybe I should try location block instead of server ? – RMP Jul 14 '16 at 07:52
  • @RMP You are not seeing the problem. http request goes into this server block and got a 301 return to https, then a redirect https request comes to nginx but WILL NOT go into this server block since it is a https request to port 443, So what ever you do in this server block will not work. So do you have a server block that serves the https thing? – Hammer Jul 14 '16 at 08:00
  • Could be this one - should I try to include rewrite rule here ? `server { listen 80; #443 ssl; server_name my.domain.com; status_zone public; client_max_body_size 20M; proxy_read_timeout 120s;` – RMP Jul 14 '16 at 08:08
  • My server block looks like this, please ignore https:// part....that is used for other subdomain. Real one: `server { listen 80; server_name domain.com; rewrite ^ http://www.domain.com permanent; }` – RMP Jul 14 '16 at 08:13
  • @RMP not this one, If you believe you have set up https correctly, there must be something like ``ssl on`` or ``listen 443`` or `listen ssl``. And i'm gonna stop here since you are asking for more then this question and it is hard for other people to search according to your title here. You should get yourself more familiar with nginx or even with the right rewrite rule you still don't know where to put. – Hammer Jul 14 '16 at 08:15