【问题标题】:Codeigniter: 301 redirect URLs from old siteCodeigniter:来自旧站点的 301 重定向 URL
【发布时间】:2015-01-22 16:15:24
【问题描述】:

我最近启动了一个 codeigniter 网站,需要进行一些从旧网站到新网站的 301 重定向。它们不适用于正则表达式,因此,由于只有少数,我可以为每个页面编写重定向。然而,经过数小时的谷歌搜索/纠正,我仍然没有快乐。旧站点的 URL 是 .html 文件,而我的 codeigniter URL 是“干净”的 URL。这是我当前的 .htaccess 文件:

<IfModule mod_alias.c>
   redirect 301 "/index.html" /index.php
</IfModule>

<IfModule mod_rewrite.c>
    DirectoryIndex index.php
    RewriteEngine on

    #This is the redirect I've been trying to make work
    RewriteRule ^/about-us.html /index.php?/about-us [R=301,L]

    RewriteCond $1 !^(index\.php|index\.html|lib|robots\.txt)
    RewriteRule ^(.*)$ index.php?/$1 

</IfModule>

有什么想法吗?

【问题讨论】:

    标签: php .htaccess codeigniter redirect http-status-code-301


    【解决方案1】:

    你可以使用:

    RewriteRule ^(about-us)\.html$ /index.php?/$1 [R=301,NC,L,QSA]
    

    或用于重定向所有旧的.html 链接:

    RewriteRule ^(.+?)\.html$ /index.php?/$1 [R=301,NC,L,QSA]
    

    编辑:基于 cmets,您只需要这条规则来处理旧 URL:

    RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
    RewriteRule ^ /%1 [R=302,L,NE]
    

    【讨论】:

    • 太棒了,谢谢!如何从重定向的 URL 中删除 index.php?
    • 没错,所以重定向与未重定向的 URL 相同。
    • 谢谢 :) 如果旧的 .html URL 与新 URL 不同,我该怎么办?
    • 那你需要具体的规则在上面的301规则之前。像这样:RewriteRule ^foo-bar-baz\.html$ /abc123 [R=301,NC,L]
    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 2012-02-06
    • 2019-07-03
    • 2012-01-27
    • 2019-08-02
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多