简体   繁体   中英

Relative Nginx Rewrite Rules

I currently have an nginx instance which is solely used to retain traffic to an old url.

server {
listen xxx.xxx.IP.IP;
server_name  blog.store.com;
if ($http_host = "xxx.xxx.IP.IP"){return 301 $scheme://store.com;}
if ($http_host = blog.store.com){return 301 $scheme://store.com/blog;}
}

This works great but I want to add a rule that allows people to navigate to a sub directory of this and retain the rest of the path they hit.

Example: right now if someone hits a link that says 'blog.store.com/page' they are taken to 'store.com/blog'

I want them to be able to hit 'blog.store.com/page' and be taken to 'store.com/blog/page'

I've tried a few of the examples from

URL Rewriting Nginx

Make nginx rewrite relative

But those and different variations of $1 do not work. Am I missing something?

Thank you in advance

Try this:

server {
  listen      xxx.xxx.IP.IP  default_server;
  return      301  $scheme://store.com;
}
server {
  listen      xxx.xxx.IP.IP;
  server_name blog.store.com;
  return      301  $scheme://store.com/blog$request_uri;
}

Richard thank you very much here is my new code it worked.

server {
listen xxx.xx.xx.xxx:80;
server_name  blog.store.com;
if ($http_host = "xxx.xx.xx.xxx"){return 301 $scheme://store.com;}
if ($http_host = blog.store.com){return 301 $scheme://store.com/blog$request_uri;}
}

Thanks again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM