【问题标题】:How to Rewrite make .htaccess for site "replacement" (old site to subfolder, new site in root) while keeping the Google Love如何重写为站点“替换”(旧站点到子文件夹,根中的新站点)制作 .htaccess,同时保持 Google Love
【发布时间】:2013-02-24 06:56:01
【问题描述】:

我在一个域上存在多年(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. 我想访问没有 www (example.com) 的域根目录上的新站点
  2. 我希望旧网站仍用作“世界”中所有旧链接的主登录页面 - 主要是 Google(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/中的新站点

【问题讨论】:

    标签: .htaccess mod-rewrite rewrite


    【解决方案1】:
    #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 文件夹中的相同文件名。

    【讨论】:

    • 谢谢!总的来说,这是一个非常好的主意。虽然我有一个旧的 CMS(Joomla 的链接如下:www.example.com/_old/component/option,com_fireboard/Itemid,186/func,view/catid,12/id,22992/#22992 和 www.example.com /_old/content/view/57/122/ ) 和新的 CMS Drupal,我想在其中为页面使用漂亮、干净的 url。所以我宁愿为我移动到谷歌“翻译”的页面做一个新的重定向。
    • 我会试试这个,因为它可能比我最初看到的完美。再次感谢。我会在测试后回来 - 虽然我需要在移动更改 URL 的页面时添加“翻译器”重定向吗?
    • 因为您使用的是 oldpage.html,所以我没有怀疑您使用的是 CMS。如果您使用的是 CMS,!-f!-d 可能无法正常工作。
    • !NOT-f-d 分别是文件存在和目录存在。所以RewriteCond %{REQUEST_FILENAME} !-f 的意思是,文件系统中没有路径为REQUEST_FILENAME 的文件。
    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2023-03-27
    相关资源
    最近更新 更多