Jump to content

301 Redirects with PHP


adipalmer123

Recommended Posts

Hi all

 

I created a site a while ago now. Using PHP and MySQL. I set it up using a windows server NOT linux.

I've come to start to do some SEO work on the site, but Google is finding two different urls. the www version and the non www version.

As I'm on a windows server I cant create a htaccess file and add some script to re-direct.

For some reason I dont have IIS installed.

 

I believe you can create a re-direct in PHP. I have done this for the index.php file. It works fine when you go to the homepage but when you click on another link to a different part of the site in Google results, it then redirects me to the homepage. This is obviously messing up my rankings. 

 

The code i used for the re-driect was as follows:

<?php

if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {

header('HTTP/1.1 301 Moved Permanently');

header('Location: http://www.'.$_SERVER['HTTP_HOST']

.$_SERVER['REQUEST_URI']);

}

?>

 

My site works with includes to the index.php file. I thought this would work but obviously not.

 

Anyone any ideas.

 

Adi

Link to comment
https://forums.phpfreaks.com/topic/235623-301-redirects-with-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.