如何使http状态代码301重定向和404错误处理工作


How to make http status code 301 redirection and 404 Errorhandling work

我在一台服务器上安装了Wordpress,有两个域指向它。计划是设置一个SEO友好的重定向,包括301重定向和404错误处理。我正在使用3个文件:Apache虚拟主机、.htaccess和php-script(http状态代码404 的错误页面

Vhost(Apache)

<VirtualHost xx.xxx.xxx.xx:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
DocumentRoot /path/to/mydomain-on-filesystem
     <Directory /path/to/mydomain-on-filesystem>
      Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
     </Directory>
</VirtualHost>

.htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www'.mydomain-to-be-redirected'.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain-with-content.com/$1 [R=301,L]
ErrorDocument 404 /404.php 

.htaccess和404.php位于文件系统上的/path/to/mydomain。

我很难使RewriteRule(301重定向)和ErrorDocument(404错误处理)一起工作。一旦重写规则到位,当我输入任何URL时,我都会得到"301永久移动"。它完全忽略http状态代码404。不幸的是,这导致谷歌(据我所知)和其他搜索引擎降低了页面排名(例如重复内容)。

我在这里错过了什么?非常感谢您的帮助。

期待你们的来信:-)

非常感谢!

S.D.

重写规则:

RewriteCond %{HTTP_HOST} ^www'.mydomain-to-be-redirected'.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain-with-content.com/$1 [R=301,L]

将www.mydomain-to-be-redirected.com的所有内容重定向到www.mydomain-with-content.com。下面的任何内容都不会被使用。

在这种情况下,如果页面不存在,www.mydomain-with-content.com将返回404,这似乎是您想要的。