【问题标题】:Htaccess look for pattern in url that end in .html and redirectHtaccess 在以 .html 结尾的 url 中查找模式并重定向
【发布时间】:2015-04-24 18:11:49
【问题描述】:

所以我在 google 中有一些 404 url​​,我想重定向用户,所以如果用户访问 http://example.com/URL-THAT-DOESN'T-EXIST-ANYMORE.HTML

我希望它们被重定向到索引页面,我的问题是我无法弄清楚模式,因为我是正则表达式的新手。

请务必注意,url 需要以 .HTML 结尾,否则不应重定向。而且我还需要在重定向 b/c 中指定主机我在同一个应用程序上有多个域

这是我尝试过的,但没有成功:

RedirectMatch 301 example.com^/(*)/?$ /

【问题讨论】:

    标签: php html apache redirect


    【解决方案1】:
    RewriteEngine On
    RewriteRule ^oldpage\.html$ http://www.newdomain.com/ [L,R=301]
    

    这只会将domain.com/oldpage.html 重定向到索引页面或第二个参数是什么。

    L 表示这是它所困扰的最终重定向,并立即将您发送出去。 R=301 表示它是永久重定向,浏览器会缓存它。我认为搜索引擎也注意到了。


    要重定向服务器上的所有 html 页面,请使用

    RewriteRule ^(.*)\.html$ http://www.newdomain.com/ [L,R=301]
    

    【讨论】:

    • 是的,我想要一般的东西,但是只有当请求的 url 以 .html 结尾时,重定向才需要发生
    • @Uffo 我已经更新了所有 html 页面。还添加了一个您不想忘记的相当重要的转义 反斜杠
    【解决方案2】:

    这个怎么样?

    RedirectMatch 301 html$ /
    

    RedirectMatch 301 (.*)\.html$ /
    

    【讨论】:

      【解决方案3】:

      如果您要重定向已移动的特定页面,您也可以改用Redirect

      Redirect 301 "/path/to/moved.html" "http://example.com/path/to/new.ext"
      

      【讨论】:

        【解决方案4】:
        ErrorDocument 404 http://example.com/
        

        你必须使用ErrorDocument最简单的其他重定向方式

        RewriteEngine On 
        RewriteRule (.+)\.html$ / [L,R]
        

        此重定向所有 .html 网址

        Other redirect methots

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-04
          • 1970-01-01
          • 1970-01-01
          • 2020-05-03
          • 1970-01-01
          相关资源
          最近更新 更多