Redirecting URL ID's using PHP
Hi,
I've recently updated a website and I'd like to redirect some ID URL's
using PHP code, I know how to do the redirect part the problem is the
if statement to only redirect url's with an id.
for example:
if domain is www.domain.com do nothing
but if domain is www.domain.com/?p=12 redirect to www.domain.com
Thanks in advance,
Kevin
www.seoptimise.com
Re: Redirecting URL ID's using PHP
kevgibbo wrote:
> Hi,
>
> I've recently updated a website and I'd like to redirect some ID URL's
> using PHP code, I know how to do the redirect part the problem is the
> if statement to only redirect url's with an id.
>
> for example:
> if domain is www.domain.com do nothing
> but if domain is www.domain.com/?p=12 redirect to www.domain.com
>
> Thanks in advance,
> Kevin
> www.seoptimise.com
>
You can do it with headers.
if ($_GET["p"]==12)
{
header("Location: http://www.domain12.com/");
exit;
}
Of yourse, you should handle the $_GET variables well (check for any
security issues and validation.
Probably create a switch function that will switch for all your values
(so you don't use switch for large number of domain redirects).
Don't forget to put this code at the very begining of the PHP FIle.
Re: Redirecting URL ID's using PHP
Armand Brahaj wrote:
> kevgibbo wrote:
>> Hi,
>>
>> I've recently updated a website and I'd like to redirect some ID URL's
>> using PHP code, I know how to do the redirect part the problem is the
>> if statement to only redirect url's with an id.
>>
>> for example:
>> if domain is www.domain.com do nothing
>> but if domain is www.domain.com/?p=12 redirect to www.domain.com
>>
>> Thanks in advance,
>> Kevin
>> www.seoptimise.com
>>
> You can do it with headers.
> if ($_GET["p"]==12)
> {
> header("Location: http://www.domain12.com/");
> exit;
> }
>
> Of yourse, you should handle the $_GET variables well (check for any
> security issues and validation.
> Probably create a switch function that will switch for all your values
> (so you don't use switch for large number of domain redirects).
> Don't forget to put this code at the very begining of the PHP FIle.
>
Sounds to me that some pages are no longer available or have moved.
Use correct headers with this or your search enigne results will drop
like a stone :-(
//Moved pages
header("HTTP/1.0 301 Moved Permanently");
header("Location: newpage.php");
// page doesn't exist
header("HTTP/1.0 404 Not Found");
exit;
--
Arjen
http://www.arjenkarel.nl
Re: Redirecting URL ID's using PHP
..oO(Arjen)
>Use correct headers with this or your search enigne results will drop
>like a stone :-(
>
>//Moved pages
>header("HTTP/1.0 301 Moved Permanently");
>header("Location: newpage.php");
The Location header requires an absolute URL.
Micha
Re: Redirecting URL ID's using PHP
Armand Brahaj wrote:
> You can do it with headers.
> if ($_GET["p"]==12)
> {
> header("Location: http://www.domain12.com/");
> exit;
> }
So basically, you have a delivery guy transporting a very heavy closet...
He has this address: www.example.com/?p=12
So he goes up the stairs, he has to do 3 floors worth of stairs (of
course the elevator is broken), with his closet, and works his ass up to
the bone in order to get there, he knocks on the door, and yells "I have
a delivery for www.exampl.." and before he even finishes the sentence,
someone answers: "No I'm sorry, you have the wrong address, it's 2
floors lower, second door on the left"...
Kind of ridicule, don't you think ?
So why, oh why, instead of playing ping pong with header requests, don't
you guys learn to properly use redirects ?
header ("Location:") is waaay overrated, just include the correct file,
and you'll be fine, and if you're not, you coded the website like an
idiot...
HTH,
S.
Re: Redirecting URL ID's using PHP
..oO(Sebastiaan 'CrashandDie' Lauwers)
>So why, oh why, instead of playing ping pong with header requests, don't
>you guys learn to properly use redirects ?
header('Location: ...'); is a redirect.
>header ("Location:") is waaay overrated
Not if you know what it does and how to properly use it.
>just include the correct file,
>and you'll be fine, and if you're not, you coded the website like an
>idiot...
If he wants to redirect some old URLs for example then just including
"the correct file" doesn't help much.
But in fact the OP didn't gave enough clear informations about what he
really wants to do. There might also be better ways using .htaccess
mechanisms.
Micha
Re: Redirecting URL ID's using PHP
On Sat, 16 Jun 2007 13:35:23 +0200, Sebastiaan 'CrashandDie' Lauwers =
<crashanddie+usenet [at] gmail.com> wrote:
> Armand Brahaj wrote:
>
>> You can do it with headers.
>> if ($_GET["p"]=3D=3D12)
>> {
>> header("Location: http://www.domain12.com/");
>> exit;
>> }
>
> So basically, you have a delivery guy transporting a very heavy closet=
....
>
> He has this address: www.example.com/?p=3D12
>
> So he goes up the stairs, he has to do 3 floors worth of stairs (of =
> course the elevator is broken), with his closet, and works his ass up =
to =
> the bone in order to get there, he knocks on the door, and yells "I ha=
ve =
> a delivery for www.exampl.." and before he even finishes the sentence,=
=
> someone answers: "No I'm sorry, you have the wrong address, it's 2 =
> floors lower, second door on the left"...
>
> Kind of ridicule, don't you think ?
Nope, it isn't. Whatsmore: it's blindingly fast, and the delivery guy wi=
ll =
now know where to deliver it's packages in future. What's ridicule is to=
=
accept the package and deliver it to your moved neighbour yourself until=
l =
the end of time, again and again and again.
> So why, oh why, instead of playing ping pong with header requests, don=
't =
> you guys learn to properly use redirects ?
>
> header ("Location:") is waaay overrated, just include the correct file=
, =
> and you'll be fine, and if you're not, you coded the website like an =
> idiot...
On a small site, fine (keep in mind the penalties from search engines fo=
r =
duplicate content though). A correct approach, especially for bigger =
sites, would be a MOVED PERMANENTLY redirect though: no duplicate conten=
t, =
legacy code, problems with relative resources etc. For logging hits it =
would also be preferable, and what's more: search engines recognize =
permanent redirects for what they are (well, at least Google does last =
time I checked), so you don't have to earn your position for the new url=
=
all over again.
If possibly, I'd opt for the webserver itself to handle this instead of =
=
PHP though, preferably in the configuration, if not possible at least in=
a =
..htaccess file.
To state 'just include the correct file...(if you're not fine) you coded=
=
the website like an idiot..' is clearly ignorant on so many levels it =
doesn't dignify an answer.
-- =
Rik Wasmus
Re: Redirecting URL ID's using PHP
kevgibbo wrote:
> Hi,
>
> I've recently updated a website and I'd like to redirect some ID URL's
> using PHP code, I know how to do the redirect part the problem is the
> if statement to only redirect url's with an id.
>
> for example:
> if domain is www.domain.com do nothing
> but if domain is www.domain.com/?p=12 redirect to www.domain.com
Even if it's possible to redirect pages in php (don't forget to give the
right error code, see other replies in this thread), but it's far better
to do this on the server instead. If you are using apache you can do
this quite gracefully and you won't need a php-redirect-page for each
page you want to have redirected, but use regexpression to pick the
pages you want to be redirected.
--
//Aho
Re: Redirecting URL ID's using PHP
Michael Fesser schreef:
> .oO(Arjen)
>
>> Use correct headers with this or your search enigne results will drop
>> like a stone :-(
>>
>> //Moved pages
>> header("HTTP/1.0 301 Moved Permanently");
>> header("Location: newpage.php");
>
> The Location header requires an absolute URL.
Whoops ur right, only 27 and memoray allready fails :-)
Arjen
--
Arjen
http://www.hondenpage.com
Re: Redirecting URL ID's using PHP
Rik wrote:
> On a small site, fine (keep in mind the penalties from search engines
> for duplicate content though). A correct approach, especially for bigger
> sites, would be a MOVED PERMANENTLY redirect though: no duplicate
> content, legacy code, problems with relative resources etc. For logging
> hits it would also be preferable, and what's more: search engines
> recognize permanent redirects for what they are (well, at least Google
> does last time I checked), so you don't have to earn your position for
> the new url all over again.
I agree, but I had in mind the recurrent problem you get with "new"
coders that is to header ("Location:"); the whole website into oblivion
just because it looks nice...
The guy doesn't even know to write a conditional statement, you really
think he's into checking his search engine ranking ? I seriously doubt that.
> If possibly, I'd opt for the webserver itself to handle this instead of
> PHP though, preferably in the configuration, if not possible at least in
> a .htaccess file.
Or just don't care ? I mean, I've never had to maintain a website top10
of certain keywords in google, so I don't give a lot, and people who
have outdated bookmarks just don't visit the site all too often, or just
specific contents, and that's the kind of user I want to have, so what
if they don't get what they want ??
Oh and btw, just the for record, if he'd just get used to naming his
variables correctly, maybe he wouldn't have needed to check if there was
a p=12 ? Just ignore the damn thing, as it isn't something he wants.
When my websites don't get correct input data, they just load the
default page, no need for any header of any kind eh...
Or maybe header ("Sorry, the designer of this program isn't an idiot,
please try to really fill in a form before sending anything");
> To state 'just include the correct file...(if you're not fine) you coded
> the website like an idiot..' is clearly ignorant on so many levels it
> doesn't dignify an answer.
Of course it is ignorant, and so what ? header ("Location:") are a waste
of time and resources on most website, because they are used
improperly, this is one of the biggest reasons...
An include will always, ALWAYS, be faster and less resources consuming
than a header ("Location:");, but you already knew that didn't you ?
S.
Re: Redirecting URL ID's using PHP
..oO(Sebastiaan 'CrashandDie' Lauwers)
>Rik wrote:
>
>> On a small site, fine (keep in mind the penalties from search engines
>> for duplicate content though). A correct approach, especially for bigger
>> sites, would be a MOVED PERMANENTLY redirect though: no duplicate
>> content, legacy code, problems with relative resources etc. For logging
>> hits it would also be preferable, and what's more: search engines
>> recognize permanent redirects for what they are (well, at least Google
>> does last time I checked), so you don't have to earn your position for
>> the new url all over again.
>
>I agree, but I had in mind the recurrent problem you get with "new"
>coders that is to header ("Location:"); the whole website into oblivion
>just because it looks nice...
>
>The guy doesn't even know to write a conditional statement, you really
>think he's into checking his search engine ranking ? I seriously doubt that.
He asked for a redirect and got a correct answer. He doesn't gave enough
informations about why he wants it, so all you can do is guess.
>> If possibly, I'd opt for the webserver itself to handle this instead of
>> PHP though, preferably in the configuration, if not possible at least in
>> a .htaccess file.
>
>Or just don't care ? I mean, I've never had to maintain a website top10
>of certain keywords in google, so I don't give a lot
If all you have to maintain is a little hobby site, then the world
couldn't care less. But there are people who want to make money with
their site, and in many cases a good SE ranking can be a very important
issue in the whole game.
>and people who
>have outdated bookmarks just don't visit the site all too often
If the site structure changes (a bad idea in general, but not always
avoidable), then you have to take care to not break incoming links.
Ignoring that and letting people run into 404s is just a sign that you
don't know what you're doing and don't care about your customers.
>or just
>specific contents, and that's the kind of user I want to have, so what
>if they don't get what they want ??
They leave. The competitor is right next door.
>Oh and btw, just the for record, if he'd just get used to naming his
>variables correctly, maybe he wouldn't have needed to check if there was
>a p=12 ? Just ignore the damn thing, as it isn't something he wants.
With properly designed URLs there wouldn't be a ?p=12 at all, but that's
another story.
>> To state 'just include the correct file...(if you're not fine) you coded
>> the website like an idiot..' is clearly ignorant on so many levels it
>> doesn't dignify an answer.
>
>Of course it is ignorant, and so what ? header ("Location:") are a waste
> of time and resources on most website, because they are used
>improperly, this is one of the biggest reasons...
I'm sure you can post some examples, how they're used improperly on most
websites?
>An include will always, ALWAYS, be faster and less resources consuming
>than a header ("Location:");, but you already knew that didn't you ?
That's not the point. And actually - what resources does a redirect
waste? Some hundred bytes network traffic and a second script call on
the server. That's not what I would call a "waste".
Micha
PHP » alt.php » Redirecting URL ID's using PHP