如何重写网站"replacement"(旧网站到子文件夹,根目录中的新网站)的.htaccess,同时保持谷歌的爱



我有一个网站在一个已经存在多年(10+)的域上,现在正在从一个CMS更改网站到另一个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. 我希望旧网站仍然作为所有旧链接的主要登陆页从"世界"-主要是谷歌(www.example.com/_old/[whatever]从www.example.com/[whatever]
  3. 我想确保,当我移动一个页面到新的网站(www.example.com/_old/oldpage.html到example.com/newpage.html),我可以重定向该URL专门到新的网站,以避免重复的内容。

我在这里:

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/

#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]

再澄清一下。如果用户请求像www.example.com/seafood.html这样的旧链接,则请求将在内部重写为/_old/seafood.html。如果将来您想为此创建一个新页面,只需在根目录中创建seafood.html即可。此文件将自动优先于/_old文件夹中的相同文件名。

最新更新