.htaccess,将 404 错误重写到其他域



如何将所有 404 错误重定向到另一个域?

我找到了

Error 404 http://example.com/error.html

但我需要:

if Error 404 (.*) http://example.com/$1

我试过:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://example.com/$1

但它重定向了所有请求,事实上,如果我运行一个.php脚本,该脚本生成一个包含一些 404 链接的页面,则该脚本的 URL 变为

http://example.com/script.php

相反,URL应该保持不变,只有404链接应该重定向到另一个域。

解决 方案?

1 - 像这样创建ErrorDocument指令:

ErrorDocument 404 /404.php

2 - 然后像这样创建您的404.php

<?php
   if ($_SERVER["HTTP_HOST"] == "domain.com") // current domain
      header('Location: http://example.com' . $_SERVER["REQUEST_URI"], TRUE, 301);
?>

仅使用 .htaccess 进行更新:

RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]

最新更新