【问题标题】:How to combine ErrorDocument and redirect in htaccess?如何在 htaccess 中结合 ErrorDocument 和重定向?
【发布时间】:2011-03-12 20:28:26
【问题描述】:

我有一个非常简单的 5 页静态网站。

每当访问者访问不存在的页面时,我想执行 301 重定向到索引页面,而不是 404。我尝试过这样的事情:

ErrorDocument 404 404.htm
redirect 301 404.htm index.htm

但这似乎不起作用。我如何让它做一个 301 重定向到索引页面?

【问题讨论】:

    标签: .htaccess redirect http-status-code-301 errordocument


    【解决方案1】:

    如果您只有一个固定的页面列表,那么您可以执行以下操作:

    #map _all_ known pages on the site
    RewriteRule url1 page1.htm [NC, L]
    RewriteRule url2 page2.htm [NC, L]
    RewriteRule url3 page3.htm [NC, L]
    RewriteRule url4 page4.htm [NC, L]
    RewriteRule url5 page5.htm [NC, L]
    
    #if there's something that doesn't fit in the rules above then it means there's no such page so we redirect to home with a 301
    RewriteRule (.+) index.htm [R=301,L]
    

    【讨论】:

    • 或:RewriteRule url([0-9]+) page$1.htm [NC, L]
    • 这只是一个例子,因为他可以有 5 个页面,分别称为 onetwothreefourfive,这些页面必须重写为 first.htmsecond.htmthird.htmfourth.htmfifth.htm。你不能用一条规则来做到这一点。
    猜你喜欢
    • 2013-11-15
    • 2011-12-01
    • 2011-11-19
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多