【问题标题】:Redirecting root / home page to custom page screws up all other redirects in .htaccess将根/主页重定向到自定义页面会破坏 .htaccess 中的所有其他重定向
【发布时间】:2014-11-27 12:41:15
【问题描述】:

我已将 2 个网站合并为一个。我已经为停用网站 (hkgolf.co.uk) 上的所有页面设置了 .htaccess 重定向,以重定向其他网站 (http://harriskalinka.com) 上的等效页面。

这是我在停用网站上的 .htaccess 中拥有的:

Redirect 301 / http://harriskalinka.com/
Redirect 301 /work/ http://harriskalinka.com/work/
Redirect 301 /projects_tag/academy/ http://harriskalinka.com/work/
Redirect 301 /projects_tag/asia/ http://harriskalinka.com/work/

我想做的是将访问旧网站主页的访问者重定向到新网站上的一个页面,告诉他们重定向的原因,并让所有其他重定向正常工作。所以我这样做了:

Redirect 301 / http://harriskalinka.com/home/hk-golf-redirect/
Redirect 301 /work/ http://harriskalinka.com/work/
Redirect 301 /projects_tag/academy/ http://harriskalinka.com/work/
Redirect 301 /projects_tag/asia/ http://harriskalinka.com/work/

现在发生的情况是,如果您转到旧网站主页 (hkgolf.co.uk/),您将被重定向到 http://harriskalinka.com/home/hk-golf-redirect/(我想要的)

问题是,如果您访问 hkgolf.co.uk/work/,您会被发送到 http://harriskalinka.com/home/hk-golf-redirect/work/ 而不是 http://harriskalinka.com/work/

为什么会这样?

【问题讨论】:

    标签: apache .htaccess redirect url-redirection


    【解决方案1】:

    重新排列您的规则以将通用规则(包罗万象)保留在最后:

    Redirect 301 /work/ http://harriskalinka.com/work/
    Redirect 301 /projects_tag/academy/ http://harriskalinka.com/work/
    Redirect 301 /projects_tag/asia/ http://harriskalinka.com/work/
    Redirect 301 / http://harriskalinka.com/home/hk-golf-redirect/
    

    您也可以使用RedirectMatch 来获得正则表达式功能:

    RedirectMatch 301 ^/work/?$ http://harriskalinka.com/work/
    RedirectMatch 301 ^/projects_tag/academy/?$ http://harriskalinka.com/work/
    RedirectMatch 301 ^/projects_tag/asia/?$ http://harriskalinka.com/work/
    RedirectMatch 301 ^/?$ http://harriskalinka.com/home/hk-golf-redirect/
    

    【讨论】:

    • 谢谢。这是否也意味着我必须对子文件夹使用相同的东西所以:Redirect 301 /blog/golf-industry/ http://harriskalinka.com/blog/golf-stuff/ 应该在之前:Redirect 301 /blog/ http://harriskalinka.com/blog/
    猜你喜欢
    • 2018-02-04
    • 2014-12-23
    • 2018-07-10
    • 2023-03-21
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2012-10-29
    • 2018-06-29
    相关资源
    最近更新 更多