首页 > 解决方案 > 将 2 个 url (%20) 重定向到 nginx 中的一个

问题描述

我有这个我无法解决的问题。我有一个 nginx 服务器,我必须进行这种类型的重定向。

http://midomain.com/xxxx/xxxx/ http://wikipedia.com/xx --> http://midomain.com/yyyy

由于我有两个带空格的网址,因此无法使其正常工作。

我尝试了多种方法。

rewrite ^/xxxx/xxxx/\ http://wikipedia.com/xx http://midomain.com/yyyy permanent;
rewrite "^/xxxx/xxxx/\ http://wikipedia.com/xx" http://midomain.com/yyyy permanent;
rewrite ^/xxxx/xxxx/%20http://wikipedia.com/xx http://midomain.com/yyyy permanent;
rewrite "^/xxxx/xxxx/%20http://wikipedia.com/xx" http://midomain.com/yyyy permanent;
rewrite ^/xxxx/xxxx/\ http://wikipedia.com/xx/$ http://midomain.com/yyyy permanent;
rewrite "^/xxxx/xxxx/\ http://wikipedia.com/xx/$" http://midomain.com/yyyy permanent;
rewrite ^/xxxx/xxxx/%20http://wikipedia.com/xx/$ http://midomain.com/yyyy permanent;

我不知道我是否做错了什么。

标签: nginxurlredirectspaces

解决方案


Nginx 在通过rewrite. 被%20解析为文字空格字符,连续/字符被单个 . 替换/。正是最后一点给你带来了问题。

例如:

rewrite "^/xxxx/xxxx/ http:/wikipedia\.com/xx$" http://midomain.com/yyyy permanent;

推荐阅读