【问题标题】:.htaccess 301 redirect of single page.htaccess 301 单页重定向
【发布时间】:2010-11-28 02:37:35
【问题描述】:

网站重新设计后,我有几个页面需要重定向。一切都在同一个域中,只有一些东西被重组和/或重命名。它们的形式是:

/contact.php

现在:

/contact-us.php

使用 .htaccess 文件,我添加了这一行,这是我最推荐的:

RedirectMatch 301 /contact.php /contact-us.php

这基本没问题 - 它可以完成工作 - 问题是,它还会重定向:

  • /team1/contact.php
  • /non-existant-folder/contact.php

有没有办法指定我只想重定向根目录中的contact.php?

【问题讨论】:

  • Dan u r a don...我想但很久以前就问过了...

标签: php .htaccess seo http-status-code-301


【解决方案1】:

它将您的商店页面重定向到您的联系页面

    <IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteBase /
    Redirect 301 /storepage /contactpage
    </IfModule>

【讨论】:

    【解决方案2】:
    redirect 301 /contact.php /contact-us.php
    

    使用redirectmatch 规则没有意义,然后必须编写您的链接,以便它们完全匹配。如果你不包括你不必排除!直接使用不匹配的重定向,然后正常使用链接

    【讨论】:

    • 简单更好。如果您不需要模式(正则表达式),那么这将是最有效的语法。
    【解决方案3】:

    如果您更喜欢使用simplest possible solution 来解决问题,RedirectMatch 的替代方案是更基本的Redirect 指令。

    它不使用模式匹配,因此更明确,更容易被其他人理解。

    即

    <IfModule mod_alias.c>
    
    #Repoint old contact page to new contact page:
    Redirect 301 /contact.php http://example.com/contact-us.php
    
    </IfModule>
    

    应该保留查询字符串,因为文档说:

    匹配的 URL 路径之外的附加路径信息将是 附加到目标 URL。

    【讨论】:

      【解决方案4】:

      RedirectMatch 使用与 URL 路径匹配的正则表达式。而您的正则表达式/contact.php 仅表示任何包含/contact.php 的URL 路径,而不仅仅是任何恰好是/contact.php 的URL 路径。所以使用锚点作为字符串的开始和结束(^ 和$):

      RedirectMatch 301 ^/contact\.php$ /contact-us.php
      

      【讨论】:

      • 当我使用类似 www.domain.com/contact.php?location=12 的内容时,它会重定向到 www.domain.com/home/mike/public_html/contact-us.php?location =12 但只有当那里有论点时。
      • 我通过在目标端输入完整的 URL(带有 http://)来修复它:“RedirectMatch 301 ^/contact\.php$ domain.com/contact-us.php”。
      • 你可以使用任何网址来代替/contact-us.php。我需要重定向到一个 IP 地址
      • 嘿,太棒了,但我想做类似的事情,但我希望有人尝试从我的主机进入私有部分然后重定向到其他页面,例如 > private.php login.php 但是在我用这个命令隐藏我的文件之前 Require all denied 那么怎么能这样做呢?
      • 请注意,这里只区分大小写
      【解决方案5】:

      如果您想要模板匹配和重定向网址的功能,也可以使用RewriteRule。

      【讨论】:

        【解决方案6】:

        应该这样做

        RedirectPermanent /contact.php /contact-us.php 
        

        【讨论】:

        • 我不明白为什么这应该可以解决问题。 RedirectPermanent … 与 Redirect 301 … 相同。
        • Gumbo,RedirectPermanent 采用 (URL-Path) 作为参数,RedirectMatch 采用 (Regex URL),因此当您使用不带 (^) 的 RedirectMatch 来表示行首时,它不起作用。使用 RedirectPermanent 也比正则表达式替代品快得多。
        • @duckyflip:对不起,你是对的。只用Redirect 误读了RedirectMatch。但是Redirect 只做前缀检查。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 2011-01-03
        • 2012-03-10
        • 2021-07-22
        相关资源
        最近更新 更多