T
T
teet2015-05-27 00:54:58
Nginx
teet, 2015-05-27 00:54:58

How to rewrite from old uri to new without redirect?

Moved an old project to a content management system written in yii ( open-real-estate.info ).
nginx config:

server {
  listen  80;
  server_name  localhost;
  charset utf-8;
  error_log  /home/user/logs/nginx.log;
  root   /home/user/html;
  index  index.php;

  location = /old-uri {
    rewrite /old-uri /service-1 last;
  }

  location / {
    root   /home/user/html;
    fastcgi_pass   unix:/var/run/php-fpm.sock;
    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;

    include        fastcgi_params;
  }

  location = /favicon.ico {
    return  204;
  }

  location ~* \.(jpg|jpeg|gif|ico|png|xml|zip|css|js|html|json|txt|swf|mov)$ {
    try_files  $uri =404;
    expires  max;
  }

  location ~ /\. {
    deny  all;
    log_not_found  off;
  }
}

The control system works great. But some links have changed (CMS features: in particular, you cannot give a specific uri for filters. Filters are now called service-1, service-2, etc.). In order not to rummage through the sources, I decided using the nginx + rewrite module to simply redirect certain links:
location = /old-uri {
    rewrite /old-uri /service-1 last;
  }

But it does not work - nginx returns 404.
Tell me how you can solve this problem without changing the CMS source codes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Softer, 2015-05-27
@Softer

You can simply specify permanent instead of last. This is if it is not scary that the link changes with the user. :)
PS: I don't think that separate locations are needed here, I usually put this (rewrite permanent) either in "location /" or even on the "server" level...

K
Konstantin Samilko, 2015-05-28
@kuroneco

You,
location = /old-uri {
rewrite /old-uri /service-1 last;
}
is not needed.
Enough,
rewrite ^/old-uri /service-1 last;
If this does not work, then perhaps when accessing the old-uri, arguments are passed that also need to be passed to / service-1
On a good note, every time the location changes, see the access log to understand what is being passed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question