Dynamic 301 redirects

I’m doing some url changes for seo purposes and would like to make a 404.php file that matches the old links against the database and sends 301 redirects. Will this work right for seo purposes or will the initial 404 error be sent before the 301.

Read up on the [fphp]header[/fphp] function. If you send a 301 redirect header you do not send a 404 document at all.

So when the htaccess file directs to the 404 page does it send information to the browser or is this action all server side?
Or maybe I should ask, What is the best way to set up the htaccess file for dynamic 301 redirects?

In your 404.php first check the database and decide if 301 redirect should be sent and then either send 301 or 404 but not both. If you do it in 404.php then don’t send 404 header in .htaccess. You can use .htaccess to tell the server to load 404.php when a page is not found (ErrorDocument) but sending the 404 or 301 header should be done in php in this case.

So when the htaccess file directs to the 404 page does it send information to the browser or is this action all server side?

No, if you use

ErrorDocument 404 /404.php

in .htaccess then simply 404.php is called and no information is sent to the browser apart from what 404.php will send itself. So send all you need to send in 404.php, it can as well send a 301 redirect to the browser.

Or maybe I should ask, What is the best way to set up the htaccess file for dynamic 301 redirects?

Now this looks like a completely different question. You can use mod_rewrite rules in .htaccess to send 404 or 301 status codes depending on certain conditions. But you can’t query the database there - you need to use php for that, so the first solution is the way to go: ErrorDocument in .htaccess + 404.php for your logic.

Lemon juice thanks for your explanation. I was afraid I was going to shoot myself in the foot sending mixed messages to search engines, but it sounds like it will work fine.

E