Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Rewrite: change only index location?

Tags:

nginx

rewrite

How can I send a user to a new location, only if the user have no URI? I am trying the follow, but it does not works... it always send me to /newlocation

rewrite ^/$ http://www.domain.com/newlocation permanent;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;

So basically, what I need is:

If the user writes on browser www.domain.org it sends to www.domain.com/newlocation If the user writes on browser www.domain.org/something it sends to www.domain.com/something

Thanks!

like image 746
PGZ Avatar asked Dec 28 '22 19:12

PGZ


1 Answers

I'm not sure why your current approach isn't working. ^/$ should only match /. Maybe it's something else it the current config. Here's a server that should do what you want.

server {
  server_name www.domain.org;

  # Only match requests for /
  location = / {
    rewrite ^ http://www.domain.com/newlocation permanent;
  }

  # Match everything else
  location / {
    rewrite ^ http://www.domain.com$request_uri? permanent;
  }
}
like image 174
kolbyjack Avatar answered Jan 03 '23 04:01

kolbyjack