【问题标题】:.htaccess Redirect If Not Subdomain.htaccess 重定向如果不是子域
【发布时间】:2012-03-03 16:52:44
【问题描述】:

我有一个 .htaccess 文件,它使用 mod_rewrite 将 URL 从旧网站重定向到新网页等效项。可以在www.eastwood-whelpton.co.uk 上查看新站点(目前仅适用于 HTML5 浏览器)。

到目前为止,我通过编写大量 Redirect Permanent 语句将每个旧页面翻译到新站点中的位置来完成这项工作。

这一直有效,直到我需要为旧网站创建一个子域(我需要保留它)。以前,它只是在文件夹 /old/ 中,但尽管 Robots.txt 不允许,但 Google 仍会抓取它。为了解决这个问题,我把它放在子域“旧”中。

现在的问题是,访问诸如 old.eastwood-whelpton.co.uk/about.htm 之类的页面仍会尝试重定向到新站点上的 old.eastwood-whelpton.co.uk/about/about.php

这是我的 HTACCESS 文件的 sn-p。我只需要重定向旧的“www”页面,而不是“旧”子域中的旧页面。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^eastwood-whelpton [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

DirectoryIndex index.php

Redirect permanent /index.htm /index.php
Redirect permanent /index.html /index.php

Redirect permanent /yachthire.htm /holidays/sailingholidays.php
Redirect permanent /yachts.htm /about/thefleet.php
Redirect permanent /handover.htm /training/handover.php
Redirect permanent /holiday.htm /holidays/familyholidays.php

【问题讨论】:

    标签: .htaccess redirect subdomain


    【解决方案1】:

    首先,Redirect 是来自mod_alias 的指令

    不要混合它们!你的Redirect 指令将执行,不管你有什么RewriteRule

    改为这样做:

    我已将您的 Redirects 更改为 RewriteRule 并对其进行了一些调整。已添加,

    RewriteCond %{HTTP_HOST} ^old [NC]
    重写规则 ^ - [L]

    这不会重定向您的任何 old 站点 URL。

    这是全部:

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^old [NC]
    RewriteRule ^ - [L]
    
    RewriteCond %{HTTP_HOST} ^eastwood-whelpton [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    DirectoryIndex index.php
    
    ReWriteRule index\.html? /index.php [L,R=301]
    ReWriteRule yachthire\.htm /holidays/sailingholidays.php [L,R=301]
    ReWriteRule yachts\.htm /about/thefleet.php [L,R=301]
    ReWriteRule (handover)\.htm /training/$1.php [L,R=301]
    ReWriteRule (holiday)\.htm /holidays/family$1s.php [L,R=301]
    

    【讨论】:

    • 优秀。它仍然有点不完整,可能来自缓存和诸如此类的东西 - 只有 /yachts.htm 重定向在一分钟内正常工作,但我认为 RewriteRule ^ - [L] 是难题的关键。一旦我让它工作,我会选择这个作为答案。谢谢!
    • @CJxD 清除浏览器缓存一次。然后尝试。以防万一也重启你的 Apache。
    • 好吧,我还是不能让它工作。唯一正确响应的是 /yachts\.htm 行。
    • @CJxD 我刚刚更新了答案。在开头删除了每个/
    • @CJxD 来自rewriterule: In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多