T
T
teet2015-08-27 17:47:53
Nginx
teet, 2015-08-27 17:47:53

How to change uri to a new one (to be processed by another location) without a redirect?

Moved the old project to the new cms ( open-real-estate ) written in yii.
There is a need to redirect old urls to new ones, but without redirecting the client.
Here are my 2 solutions:
1.

rewrite ^/cg-([0-9]*)[/]*$ /property/cg-$1 permanent;

it works, but it's a redirect (not happy with it).
2.
location ~ ^/cg-([0-9]*)[/]*$ {
    try_files $uri /property/cg-$1;
  }

this does not work.
Below is the complete nginx config.
server {
  listen  80;
  server_name  localhost;
  charset utf-8;
  error_log  /home/costa/logs/nginx.log;

  root   /home/costa/html;
  index  index.php;

#  rewrite ^/cg-([0-9]*)[/]*$ /property/cg-$1 permanent;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ ^/cg-([0-9]*)[/]*$ {
    try_files $uri /property/cg-$1;
  }

  location ~ \.php$ {
    include  fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
    fastcgi_pass  unix:/var/run/php-fpm.sock;
  }

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2015-08-27
@teet

I suggest to combine:

location ~ ^/cg-([0-9]*)[/]*$ {
    rewrite ^/cg-([0-9]*)[/]*$ /property/cg-$1 break;
    try_files $uri @php;
}

location @php {
    include  fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
    fastcgi_pass  unix:/var/run/php-fpm.sock;
}

P
Pavel Volintsev, 2015-08-27
@copist

See the documentation for nginx/rewrite
If the "to" rule does not include the scheme (http:// or https://) , then you can do an internal redirect.
Files
/old/path/index.html with the text "Old";
/new/path/index.html with the text "New";
Try this rule

location ~ /old/path {
    rewrite /old/path(.*) /new/path$1 break;
}

My browser remained with the same URL, but showed the file in a new path
download?id=jNnoITrzRcwF9wcxezeF8AzSoWp6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question