ErrorDocument 404到外部URL

时间:2017-01-30 19:16:18

标签: wordpress apache .htaccess redirect

我们已将网站移至新域名,出于搜索引擎优化目的,我们需要将所有404重定向到新域名。

Our website domain : example.com
Moved to: new-domain.com/example

域名的任何不存在的网址,例如example.com/non-existentnew-domain.com/example/non-existent

我尝试在我的.htaccess文件中使用以下内容并且它不起作用。

ErrorDocument 404 http://new-domain.com/example/%{REQUEST_URI}

这是一个WordPress安装,除了用WordPress钩子处理这个,还有一个htaccess解决方案吗?

我使用Apache版本2.4.18在Ubuntu 16.04上

感谢。

更新

@Panama Jack提供的解决方案存在一个问题,这是我的实际.htaccess文件

RewriteOptions inherit

# 301 Redirects
redirect 301 /kanban-boards.html http://www.digite.com/swiftkanban/
redirect 301 /kanbanboards.html http://www.digite.com/swiftkanban/
redirect 301 /lp/kanban.html http://www.digite.com/swiftkanban/
redirect 301 /project-collaboration.html http://www.digite.com/swiftkanban/
redirect 301 /team-collaboration.html http://www.digite.com/swiftkanban/
redirect 301 /webinar/enterprise-services-planning.html http://www.digite.com/swiftkanban/enterprise-services-planning-with-kanban/
redirect 301 /cookie-policy/ http://www.digite.com/
redirect 301 /swiftkanban-subscription-confirmation http://www.digite.com/swiftkanban/subscription-confirmation/

#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?swiftkanban\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ http://www.digite.com/swiftkanban%{REQUEST_URI} [R=301,L]

# BEGIN WPSuperCache
# END WPSuperCache

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

最后两个301重定向不再有效,此网址http://swiftkanban.com/cookie-policy/被重定向到http://www.digite.com/swiftkanban/cookie-policy/,而我希望它会重定向到http://www.digite.com/,同样适用于swiftkanban.com/swiftkanban-subscription-confirmation < / p>

它似乎与定义的404规则相冲突,因为当我禁用规则时,它似乎有效。

1 个答案:

答案 0 :(得分:2)

您无法将错误文档与服务器变量混合在一起。您可以尝试使用mod-rewrite。将此规则置于当前的wordpress规则之上。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old-example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ http://www.new-example.com/example%{REQUEST_URI} [R=301,L]