user1642018
user1642018

Reputation:

How to use Errordocument 404 with 301 in header?

I have a dynamic content website and I want to redirect all non existed pages to index page but with the header showing 301 permanent redirect and not the 404 error.

I know that I can redirect with .htaccess

Errordocument 404 /index.php

But this will have the 404 status in the header, and I'd rather the header to show a 301 redirect.

Is this possible?

Upvotes: 2

Views: 4990

Answers (1)

SáT
SáT

Reputation: 3692

First of all, you shouldn't. If some content is not found, the server should return 404; you should only return 301 only if the content has been moved.

That said, it's possible, using the mod_rewrite module.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [R=301]

This should redirect requests to URIs that aren't files or directories to the index.php, with a 301 header.

But then again, don't. It's guaranteed to confuse human visitors and search engines alike.

Upvotes: 3

Related Questions