【问题标题】:How to write an htaccess 301 Redirection rule to redirect from php to html如何编写 htaccess 301 重定向规则以从 php 重定向到 html
【发布时间】:2014-05-21 07:58:28
【问题描述】:

我有一个问题(这可能是一个简单的问题,但我从来不需要编写正则表达式)

一位 SEO 专家告诉我要制作漂亮的 URL,所以我使用了 CMS 提供的 .htaccess 文件。 但现在他要求我将旧网址重定向到新网址。

这行不通

RewriteRule ^index.php?page=kontakt$ /kontakt.html  [R=301,L]

还有这个(应该从 index.php 文件重定向到主页)

RewriteRule ^index.php$ /   [R=301,L]

导致sitename.com/?page=kontakt,所以现在我也必须重定向它。

我该如何解决这个问题?

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    RewriteRule 仅匹配没有查询字符串的基本 URL。你需要一个额外的 RewriteCond 才能工作。

    RewriteCond %{QUERY_STRING} ^page=kontakt$
    RewriteRule ^index.php$ /kontakt.html [R=301,L]
    

    编辑: 显然在这种情况下查询字符串被保留,所以你可能会得到 /kontakt.html?page=kontakt

    要丢弃原始查询字符串,您需要在 URL 后面加上 ?

    RewriteRule ^index.php$ /kontakt.html? [R=301,L]
    

    【讨论】:

    • 非常感谢您的回答,但这给了我一个奇怪的结果:sitename.com/kontakt.html?page=kontakt 是否可能与服务器设置有关?
    • 它有效,救了我的命。我多年来一直在为此苦苦挣扎。非常感谢:D
    • 最后一件事,我如何在没有 index.php 文件的情况下也重定向/?page=kontakt
    • 只需使用 ^$ 代替 ^index.php$
    猜你喜欢
    • 2012-11-07
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多