大网数据广告活动
网站运营与故障问答

Redirect all requests to a subdomain except the root URL and index page under nginx

· 52CE 我爱测

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…

问题原文

原帖链接 · 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-10-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。