Forum Moderators: open

Message Too Old, No Replies

how to redirect php pages.

         

Gemini23

9:48 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, I need to redirect a number of php generated pages to individual html pages... my php generated format is as such.. (and shows as such in the serps)

/index.php?pr=Home_Page
or
/index.php?pr=xyz

and when I try to add a redirect then ALL of the index.php pages are redirect to one page.. regardless of their actual page name...

any suggestions?

g1smd

10:31 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How are you doing that redirect?

If you are using Mod_Rewrite code in .htaccess then you need a RewriteCond to test the %{QUERY_STRING} as well.

Gemini23

10:46 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was trying to use the main redirect in the control panel...
but obviously is very limited.. I don't have experience of Mod_rewrites...

g1smd

11:03 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to read some of the threads in the Apache forum; especially the stuff linked from the sticky threads.

What you want to do, or something very similar, is the subject of at least several conversations in that forum every week.

webfoo

11:45 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



The easiest way to do a redirect in PHP is by placing the following code at the top of your page:

<?php
header( 'Location: redirect_destination.html' ) ;
?>

where redirect_destination.html is the URL of the index.html page.

That method is easy to implement for a few pages, but if you are working on dozens or hundreds of pages, you should probably go with server mods.

g1smd

12:19 am on Dec 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That example gives a dangerous 302 redirect.

You need an extra line to convert that to a 301.

HEADER ("HTTP/1.1  301 Moved Permanently");

mcneely

12:35 pm on Jan 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This one usually does the trick for me;

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^folder/index.php$ "http\:\/\/www\.example\.com\/" [R=301,L]

g1smd

8:06 pm on Jan 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's several typos in that code.

The Condition is redundant; not needed at all.

You need to escape the period on the left of that rule.
Don't escape the colon, slashes or periods in the literal URL on the right.

It doesn't do what the OP needs.

Sure it does do something useful, and is needed in some form on almost every site.

mcneely

4:53 am on Jan 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's several typos in that code.

Well then, don't you think someone should tell Apache about it? LOL

g1smd

7:22 pm on Jan 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why would they be interested in that?

Or is that code from an example somewhere on their site?

As I said, in that example the pattern to be matched needs some characters escaping, and there should not be any escaping added to the literal target URL. That's a syntax error, or seven, in that one example.

mcneely

5:51 am on Jan 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Or is that code from an example somewhere on their site?

Actually, no ... it was generated via cPanel Pro on a 'Nix box.

Automation, you know ... can just be damn handy ... or not :)

g1smd

10:47 am on Jan 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh. It could be that everyone on that host has faulty code in their .htaccess file now.

Have you got any way to let them know about that?

mcneely

10:43 am on Jan 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can't you just hear my finger tips thrumping on my desk?

Nix/Apache says it's a cPanel problem - I'm guessing that their (cPanel) latest release has been a bit buggier than a windscreen on a sunny summer afternoon these days.

I've gone to writing some of this stuff myself, at least for the ones who want it done;

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^folder/index.php$ http://www.example.com/ [R=301,L]

I'm still waiting to hear back from cPanel.
I'd hate to change. We ran Plesk for a period of time a while back on a few windows boxes and any of our clients that used the cPanel before hated it ... needless to say, Plesk went away.

g1smd

7:14 pm on Jan 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add escaping to the period in the pattern.

Note that

^.*$
simplifies to
.*
too.

Add a trailing question mark to the target URL to also cancel query strings on the end of the original URL.

mcneely

7:50 am on Jan 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah yes then ... I've got it.

It's been a bit of a long while since I've taken the plunge on writing these in.

At any rate, the mates over on the cPanel suggested that I take a bot trap or two down, on a few of the effected domains. Cleared up quicker than a bad case of acne.

Still, I've a mind to write most of these types of redirects in by hand just to stay on my game in the future.

Good one on you, and thanks g1smd