如何重写make .htaccess为站点“替换”(旧站点到子文件夹,root中的新站点)同时保持Google Love

时间:2013-03-08 16:38:20

标签: .htaccess mod-rewrite rewrite

我在域上存在多年(10+)的网站,现在正在将网站从一个CMS更改为另一个。移动内容是一个漫长的过程,其中一些内容不会移动,并且应该在未来的几年内共存。

我已将旧网站从根目录(www.example.com)移至子文件夹(www.example.com/_old)。旧网站有以下.htaccess强制www。在我的域名自2007年初以来。我已在(www.example.com/_old/)文件夹中的.htaccess上删除了此内容。

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

现在我在另一个子目录(www.example.com/dp/)中说新闻网站,只用.htaccess和重写保持根空,但我真正想要的是:

  1. 我想访问w / o www(example.com)
  2. 域名根目录下的新网站
  3. 我希望旧网站仍然可以作为“世界”所有旧链接的主要目标网页 - 主要是Google(www.example.com/_old/ [whatever]来自www.example.com/ [whatever]
  4. 我想确保当我将网页移动到新网站(www.example.com/_old/oldpage.html到example.com/newpage.html)时,我可以将该网址专门重定向到新网站避免重复内容。
  5. 我在这里:

    RewriteCond %{HTTP_HOST} www.example.com
    RewriteRule ^(.*)$ /_old/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} example.com
    RewriteCond %{REQUEST_URI} /dp/
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=307]
    
    RewriteCond %{HTTP_HOST} example.com
    RewriteRule ^(.*)$ http://www.example.com/_old/$1 [L,R=301]
    

    这仍然是子网站/ dp /

    中的新网站

1 个答案:

答案 0 :(得分:0)

#if file or directory doesn't exist, check the _old directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/_old
RewriteRule .* /_old/$0 [L]
#note that I omitted the redirect flag. That way the old url just work like they used to. If you really want you can just add them, but please, just give it a try. This might even fix problem nr. 3

# remove www. from everything except _old urls
RewriteCond %{HTTP_HOST} www\.example\.com
RewriteCond %{REQUEST_URI} !/_old
RewriteRule ^(.*)$ http://example.com/$1 [L,R=307]

只是为了澄清一点。如果内部重写到/_old/seafood.html,用户请求www.example.com/seafood.html之类的旧链接。如果将来您想为此创建新页面,只需在根目录中创建seafood.html。此文件将自动优先于/ _old文件夹中的相同文件名。