gpt4 book ai didi

redirect - 如何仅重定向位于 nginx map 中的 url

转载 作者:行者123 更新时间:2023-12-01 02:25:53 24 4
gpt4 key购买 nike

我正在将我的博客从 a.com 移出至b.com .现在我想告诉 Google/bookmarks 所有博文(大约 100 篇)已移至 b.com .我只想重定向博文而不是别的。

在阅读了 map 之后nginx中的模块我尝试了以下内容:

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

当我将以下行放入 server 时堵塞:
rewrite ^ $new redirect;

它将重定向所有 100 个帖子,但我域上的所有其他页面都会出错: 302 Found .

这是我在配置中的整个服务器 block :
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;
}

我认为 map 会干扰 try_files电话咨询 \ (我需要)。

最佳答案

我为您的问题找到了解决方案on serverfault :

This is probably failing because you're trying to redirect all requests, whether they matched something in the map or not.

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

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


所以更换
rewrite ^ $new redirect;


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

你应该很高兴

关于redirect - 如何仅重定向位于 nginx map 中的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com