문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top