.htaccess 使用通配符重定向/重写

.htaccess Redirect / Rewrite with wildcards

在过去的一年里,我一直在使用 WordPress 主题,这迫使我合并了一系列 301 重定向,以便用户可以通过页面导航查看后续页面。也就是说,主页的 URL 将是 http://example.com, when the user presses the page navigation arrow http://example.com/page/2/ is attempted to be loaded, but in order to view the content the user must be redirected to http://example.com/main/page/2/

Redirect 301 http://example.com/page/*/ http://example.com/main/page/*/

以下代码在用户位于第一页以外的页面并使用页面导航时有效,但如果他们从主页开始则无效。也就是说,如果用户在 http://example.com/main/page/2/ and use the page navigation they will be properly redirected to http://example.com/main/page/3/. However if they are on the main page and attempt to use the page navigation they will be sent to http://example.com/page/2/ 有什么想法吗?

你的 Redirect 指令应该是

Redirect 301 /page http://example.com/main/page

Redirect 不支持像 RedirectMatch 这样的完整模式匹配,因此无法识别 * 但它仍然可以处理简单的前缀匹配,即 /page 之后的任何内容自动添加到您的目标 URL。而且,就像我上面的例子一样,旧的 URL 路径以 / 开头。