首页 > 解决方案 > 如果不正确,Apache 重定向到另一个子目录

问题描述

我有这样的文件夹结构

public_html/
  test/
    right/
      file.json
    wrong/
      file.json

因此,例如 example.com/test/right/file.json 是一个有效的 url。

我想要做的是,如果他们在 test 中遇到任何其他问题/他们将被重定向到 wrong/file.json

所以例如

example.com/test/right/file.json -> .../right/file.json
example.com/test/blah/file.json -> .../wrong/file.json
example.com/test/whatever/file.json -> .../wrong/file.json

我试过这样的事情:

RedirectMatch 301 ^/((?!right).*)$ http://example.com/test/wrong/file.json

但我刚刚进入重定向循环。

标签: apache.htaccessredirect

解决方案


您可能正在寻找ErrorDocument

ErrorDocument 404 http://example.com/test/wrong/file.json

只允许right/file.json

RedirectMatch 301 ^/(?!(right|wrong)/file\.json$) http://example.com/test/wrong/file.json

推荐阅读