1
votes

I'd like to get this straighten out once and for all:

<?php if ($_SERVER['HTTP_HOST'] != 'domain.com'){
header("Location: http://domain.com"
.$_SERVER['REQUEST_URI']);
} ?>

This will send all traffic to the NON-WWW version.

My question is - can I add 'header('HTTP/1.1 301 Moved Permanently');' safely without messing everything up?

I'm using IIS server so .htaccess is no good and all pages are coded in PHP - all solutions welcome.

2

2 Answers

2
votes

Of course you can. Take advantage of all the parameters offered by header():

<?php

if ($_SERVER['HTTP_HOST'] != 'domain.com'){
    header("HTTP/1.1 301 Moved Permanently", true, 301);
    header("Location: http://domain.com".$_SERVER['REQUEST_URI']);
}

The last parameter is forcing the response code to be 301, as you want.

0
votes

Of course you can, it will not mess up anything! But it must be in the if clause behind the header location line:

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

But better if you use URL Rewrite (aka "IIS mod_rewrite"): http://www.iis.net/downloads/microsoft/url-rewrite Or some other redirect not in your php code.