【问题标题】:.htaccess - Redirect old website to main domain but also some content to subdomain of the main domain.htaccess - 将旧网站重定向到主域,但也将一些内容重定向到主域的子域
【发布时间】:2021-04-16 11:37:29
【问题描述】:

我想将旧域(https 和 www)重定向到主域。我需要将其重定向到主页和博客。但是,我想重定向到主页面的子域的旧站点上有 4 个页面。

它应该是什么样子:

Oldwebsite1.com -> mainwebsite2.com

Oldwebsite1.com/blog -> Mainwebsite2.com/blog

Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1

Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2 /// 等等...

像这样的典型代码会起作用还是我需要对其进行调整?

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.mainwebsite2.com/$1 [R=301,L]

【问题讨论】:

    标签: .htaccess redirect mod-rewrite subdomain http-status-code-301


    【解决方案1】:

    根据您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。这些也处理您主机名中的可选www

    RewriteEngine ON
    ##For Oldwebsite1.com -> mainwebsite2.com
    RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
    RewriteRule ^/?$ http://mainwebsite2.com [R=301,L]
    
    ##For Oldwebsite1.com/blog -> Mainwebsite2.com/blog
    RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
    RewriteRule ^(blog)/?$ http://mainwebsite2.com/$1 [NC,R=301,L]
    
    ##For Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1 OR Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2
    RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
    RewriteRule ^(category1|category2)/?$ http://new.mainwebsite2.com/$1 [NC,R=301,L]
    

    【讨论】:

    • 我认为 OP 想要 new.mainwebsite2.com for category1|category2
    • @anubhava,谢谢先生,我现在已经改了,干杯。
    • @Zuzka,您能否发布无效的示例网址,然后告诉我。
    • @Zuzka,好的,将第 8 行的规则更改为 RewriteRule (.*blog.*)/?$ http://mainwebsite2.com/$1 [NC,R=301,L] 一次怎么样?请让我知道情况如何。我当前的规则查看 uri 是否从 block 开始,但此规则将检查 uri 是否包含 block 然后执行重定向。
    • @Zuzka,如何使用以下两条规则更改第 8 行 RewriteCond %{REQUEST_URI} blog [NC] RewriteRule ^(.*)/?$ http://mainwebsite2.com/$1 [NC,R=301,L](尽管这些是在 cmets 中显示单行的 2 条不同的行)一次,让我知道。请确保您也将第 7 行的 ^(?:www\.)Oldwebsite1\.com$ [NC] 更改为您的实际域,干杯。
    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2020-10-26
    • 2017-07-10
    • 2016-06-24
    相关资源
    最近更新 更多