问题原文
原帖链接 · qbi · CC BY-SA 4.0
Up until now, our website consisted of a wiki with several wiki sites. We now decided to host some general information on a single page and move the wiki to a subdomain (https://wiki.example.com/). I want to redirect nearly all requests from https://example.com/XYZ to https://wiki.example.com/XYZ, only requests to https://example.com/ and https://example.com/index.php should not be redirected.
The idea here is that people who have bookmarks to https://example.com/FOO get redirected to the correct page at https://wiki.example.com/FOO as well as people who come from search engines.
How can I achieve this with nginx?
已采纳解答
原帖链接 · Stephen Ostermiller · CC BY-SA 4.0
One way to do this is to remove all the files from your document root except for index.php and remove all wiki configuration from your ngnix configuration for example.com. Then https://example.com/ and https://example.com/index.php will return the correct page and all other requests will return a 404 error.
Then you can set your 404 handler to do the redirect:
error_page 404 =301 http://wiki.example.com$request_uri;
Tested on Nginx 1.18.0
Source: Module ngx_http_core_module
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
