How to redirect urls located on nginx map

I am moving my blog from b.com to b.com . Now I want to tell Google / bookmarks that all blogposts (about 100) have been transferred to b.com . I just want to redirect blogposts and nothing more.

After reading the map module in nginx, I tried the following:

 map_hash_bucket_size 128; map $uri $new { /post http://b.com/post # (repeated for all 100 posts) } 

And when I put the following line inside the server block:

 rewrite ^ $new redirect; 

It will redirect all 100 messages, but all other pages in my domain will be erroneous: 302 Found .

Here is my entire server block inside config:

 server { listen 80; server_name b.com; root /my/old/path/; index index.html; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } # example.com/index gets redirected to example.com/ location ~* ^(.*)/index$ { return 301 $scheme://$host$1/; } # example.com/foo/ loads example.com/foo/index.html location ~* ^(.*)/$ { try_files $1/index.html @backend; } # example.com/a.html gets redirected to example.com/a location ~* \.html$ { rewrite ^(.+)\.html$ $scheme://$host$1 permanent; } # anything else not processed by the above rules: # * example.com/a will load example.com/a.html # * or if that fails, example.com/a/index.html location / { try_files $uri.html $uri/index.html $uri @backend; } # default handler # * return error or redirect to base index.html page, etc. location @backend { try_files /404.html 404; } location ~ \.php$ { expires off; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass php; } # apply the rewrite map rewrite ^ $new redirect; } 

I think the map is interfering with calling try_files for location \ (which I need).

+4
source share
2 answers

I found a solution for your problem on serverfault :

This probably doesn’t work because you are trying to redirect all requests whether they matched something on the map or not.

To prevent this, check if there was a first match.

 if ($new) { return 301 $new; } 

So replace

 rewrite ^ $new redirect; 

with

 if ($new) { return 301 $new; } 

And you should be good to go

+3
source

I was not able to figure out how to do this with the map, since for me it is still a very string module. Now I just put all the rewrites into a list inside my server block (primarily the location blocks), which is probably very slow. Now the file looks like this:

 server { listen 80; server_name a.com; root /my/old/path/; index index.html; rewrite /post http://b.com/post # (x100) location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } # example.com/index gets redirected to example.com/ location ~* ^(.*)/index$ { return 301 $scheme://$host$1/; } # example.com/foo/ loads example.com/foo/index.html location ~* ^(.*)/$ { try_files $1/index.html @backend; } # example.com/a.html gets redirected to example.com/a location ~* \.html$ { rewrite ^(.+)\.html$ $scheme://$host$1 permanent; } # anything else not processed by the above rules: # * example.com/a will load example.com/a.html # * or if that fails, example.com/a/index.html location / { try_files $uri.html $uri/index.html $uri @backend; } # default handler # * return error or redirect to base index.html page, etc. location @backend { try_files /404.html 404; } location ~ \.php$ { expires off; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass php; } # apply the rewrite map rewrite ^ $new redirect; } 
0
source

Source: https://habr.com/ru/post/1480273/


All Articles