简体   繁体   中英

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

I have a Wordpress-installation on a server with two domains pointing to it. The plan is to set up a SEO-friendly redirect including 301-redirection and 404 error-handling. I am using 3 files: Apache virtua-host, .htaccess and php-skript (error page for http status code404

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 and 404.php are located /path/to/mydomain-on-filesystem.

I have trouble making the RewriteRule (301 redirect) and ErrorDocument (404 error handling) work together. As soon as the RewriteRule is in place I get "301 move permanently" when I enter any URL. It completely ignores http status code 404. Unfortunately that causes Google (as far as I heard) and other search engines to decrease the Page Ranking (eg duplicate content).

What am I missing here? Any help is greatly appreciated.

Looking forward to read from you guys :-)

Thanks a lot!

SD

The rewrite rules:

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

redirects everything from www.mydomain-to-be-redirected.com to www.mydomain-with-content.com. Anything below that will not get used.

In this case, www.mydomain-with-content.com will return the 404 if the page is not there, which seems like what you would want to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM