Forum Moderators: coopster
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
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.
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.
$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;
}