tags:

views:

54

answers:

1

What is the best way to compare a current website url in your address bar with the url saved in the database?

What I want to do is make it so if the url in the database is http://www.domain.com/ but the user types in http://domain.com/ it will 301 redirect to the domain saved in the database...

The same will happen if the domain saved is http://domain.com/ and they enter http://www.domain.com/... I want it to redirect to http://domain.com/

I don't want to do this with htaccess. I want to actually build it into my PHP script. Wordpress has done this but I can't find the code that they use to do it...

Thanks for your help!

A: 

Something like this might work.

<?php
if ('http://' . $_SERVER['HTTP_HOST'] . '/' != $url_from_db) {
   header('HTTP/1.1 301 Moved Permanently'); 
   header('Location: ' . $url_from_db . $_SERVER['REQUEST_URI']);
}
?>
Mark Stewart