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?

  • I'm confused. You want to redirect a all requests from example.com/ to wiki.example.com/ but you also say "only requests to example.com/ and example.com/index.php should not be redirected? Can you please describe the desired experience from a user's perspective? If this is wordpress, what you describe breaks wp-admin/ what about static assets? css, js, jpg/png/gif. Oct 1, 2021 at 22:25
  • I tried to add some more information. Hope it is helpful.
    – qbi
    Oct 1, 2021 at 22:49
  • only 301 for each individual page. remember, that example.com/index.php can generate example.com/index.php/foo and example.com/index.php/foo/bar Oct 1, 2021 at 23:21

2 Answers 2


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


In the .htacess file in public_html of https://example.com/ write:

Redirect 301 /XYZ https://wiki.example.com/XYZ

It should do what you need

  • This doesn't appear to apply to nginx, and I believe it misunderstands the redirect situation. Oct 2, 2021 at 5:15
  • nginx doesn't have .htacccess files, only Apache has those. Oct 2, 2021 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.