【问题标题】:.htaccess redirect subdomain, with exceptions.htaccess 重定向子域,有例外
【发布时间】:2016-02-18 00:08:33
【问题描述】:

在更新我的网站时,我试图将子域重定向到我最近创建的静态 WordPress 页面:story.amcorbin.com --> amcorbin.com/stories

但是,我在该子域上有特定页面,这些页面已经链接到其他地方并且不受我的控制,我不想导致链接断开。示例:story.amcorbin.com/baseofthecomet.html

我设法让 htaccess 重定向子域,但我无法让它应用异常条件。这是我的位置:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{REQUEST_URI} !^/baseofthecomet.html [NC]
RewriteCond %{REQUEST_URI} !^/digital-witnesses.html [NC]
RewriteCond %{REQUEST_URI} !^/inheritance.html [NC]
RewriteCond %{REQUEST_URI} !^/intelpost.html [NC]
RewriteCond %{REQUEST_URI} !^/silver.html [NC]
RewriteCond %{REQUEST_URI} !^/silverandgold.html [NC]
RewriteCond %{REQUEST_URI} !^/the-fairy-woods.html [NC]
RewriteRule ^(.*)$ http://www.amcorbin.com/stories$1 [L,R=301]

Redirect 301 stories.amcorbin.com http://amcorbin.com/stories/

我已经对 RegEx 做了一些修改,但我对此很不自信。我还想,哦,我只是移动或复制所有我想保留的页面......除了它有它自己的问题。

作为 htaccess 的一个完全新手,我很惊讶我已经设法做到了我所拥有的,但我已经无计可施了。谁能帮我克服这最后一道障碍?

【问题讨论】:

    标签: wordpress .htaccess redirect


    【解决方案1】:

    您不需要使用 Redirect 301,只需 R=301 的 RewriteRule 就足够了。

    我看到你在 htaccess 中使用了 ^/,%{REQUEST_URI} 不包括 / 在这个字符串的开头。

    所以请尝试:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/images/ [NC]
    RewriteCond %{REQUEST_URI} !^/baseofthecomet.html [NC]
    RewriteCond %{REQUEST_URI} !^/digital-witnesses.html [NC]
    RewriteCond %{REQUEST_URI} !^/inheritance.html [NC]
    RewriteCond %{REQUEST_URI} !^/intelpost.html [NC]
    RewriteCond %{REQUEST_URI} !^/silver.html [NC]
    RewriteCond %{REQUEST_URI} !^/silverandgold.html [NC]
    RewriteCond %{REQUEST_URI} !^/the-fairy-woods.html [NC]
    RewriteRule ^(.*)$ http://www.amcorbin.com/stories/$1 [L,R=301]
    

    谢谢。

    【讨论】:

    • 在 httpd.conf 中,REQUEST_URI 确实有一个前导 /。但在 htaccess 中,它不包含前导 /。
    • 是的,你是对的,但在两种情况下 RewriteCond 都需要它。
    • 你说得对,在 RewriteCond 中,它以 / 开头。我编辑了答案。您应该在 $1 之前添加一个斜杠。浏览器会记住旧的 301 规则,所以我认为您应该通过私人窗口对其进行测试,或者在重试之前删除所有缓存和历史记录。
    • 非常感谢你们!编辑后的答案完美无缺。
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多