Question: I'm having a strange issue with a permanent redirection in PHP. 

Here's the code I'm using:

```
if ($_SERVER['HTTP_HOST'] != 'www.mydomain.ca')   
{
    header("HTTP/1.1 301 Moved Permanently"); 
    $loc = "http://www.mydomain.ca".$_SERVER['SCRIPT_NAME'];
    header("Location: ".$loc);
    exit;
}

```

So, the home page, referenced either by www.myolddomain.ca or www.myolddomain.ca/index.php both work but every other page on the site fails to redirect. I've spent a couple of hours looking at this from all the angles I know and can't fathom it. Does anyone have any idea what the issue could be?

As a note, I've tried this without the 301 header too and get the same issue.

Answer: 
If you have a ftp-account to the server, you can create a .htaccess file and let it handle the requests. Just create a file named `.htaccess` in the root-folder of your site and post the code, changed to your desired pattern(s)

```
Options +FollowSymLinks
RewriteEngine on
RewriteCond {HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

```

If you want to do it fix via `PHP`, I would create a `redirect.php` and include it on every site you need it. Hard to tell if this is the best solution, it is a bit depending on your way of layouting and structuring.
Try it by htaccess, create .htaccess file and try something like this 

RewriteEngine on
RewriteRule (.\*) <http://www.newdomain.com/>$1 [R=301,L] 

\*\* Apache Mod-Rewrite moduled should be enabled