Forum Moderators: coopster

Message Too Old, No Replies

Achieving 301 Redirection using the header()

having used mod_rewrite I am now left with var=foo strings in SEs I do not

         

TravelMan

11:57 am on Jul 7, 2003 (gmt 0)

10+ Year Member



Hello

I recently used mod_rewrite to clean up some dynamically created content.

Which handles page.php?var=foo and rewrites as /var/foo

I want people who find page.php?var=foo in search engines to be sent to the new url.

A quick way of doing this ( I thought) would be to put a simple little bit of code into the old page which redirects people to the new.


if($var){
header( "Location: http://www.domain.tld/var/$var");
}

if($var2){
header( "Location: http://www.domain.tld/var2/$var2");
}

The above works fine, with the exception that it returns a 302 (temporarily moved) . I'd like it to return a 301 (moved permanently).

If anyone can offer up a suggestion id be most grateful.

regards

TravelMan

jatar_k

5:45 pm on Jul 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think I would set up the 301's via apache as opposed to php.

this may be informative
[httpd.apache.org...]

not sure if that is the best link but you could do some reading on apache.org and that will give you more info.

There was a thread about headers somewhere around but I can't seem to find it but I believe something like this should work.

header("HTTP/1.0 301 Moved Permanently");
header( "Location: http://www.domain.tld/var2/$var2");

just send the redirect type first.
PHP header function [ca.php.net]

Take a look at the user comments in the above link as well. They often have the most valuable info.

TravelMan

6:02 pm on Jul 7, 2003 (gmt 0)

10+ Year Member



Thanks very much Jatar_k.

Ill certainly look at that and report back with the solution I eventually use.

TravelMan

10:42 am on Jul 8, 2003 (gmt 0)

10+ Year Member



Hi Jatar

I opted for your solution

header("HTTP/1.0 301 Moved Permanently");
header( "Location: [domain.tld...]

which works just fine.

I didnt realise that you can send multiple headers -

I know now though. :)

Cheers.

ams_david

12:49 pm on Jul 9, 2003 (gmt 0)

10+ Year Member



I use this code snippet to make sure a page
is always accessed via the "real url":


$real_url = "http://example/url/";
$user_url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
if( $user_url!= $real_url ) {
header("Location: $real_url");
header("HTTP/1.0 301 Moved Permanently");
exit;
}