View Full Version : Need script to re-direct when coming from specific URL

01-19-2007, 05:28 PM
I have an old web site that is automatically being re-directed to my new site. The problem is that the address field in the browser still shows the old URL. This is a problem for Verisign. I need a script that will check to see if the address field contains my old URL and if so either change the contents of the address field OR do a link to my new URL during ONLOAD after checking for the old URL. I'm assuming this link would put the proper URL in the address field. I not very experienced in writing code so I'd appreciate any suggestions containing all the code needed. My hosting site allows me to insert HTML or Javascript into my homepage. Thanks

01-19-2007, 06:09 PM
Not gonna happen. The page isn't "still showing the old URL".... the page is still AT the old URL.
Unless I misunderstood the situation, it sounds like you are using a frame to redirect. That isn't the best way.
The two solutions are:
1. redirect better (if this is something you can control), such as with (in order of preference) location header, meta tag (<meta http-equiv="refresh" content="0;url=http://next.page/url/etc"> -- the number is seconds before refresh/redirect, if url is ommitted or blank, it defaults to the current page), or a javascript redirect (several options here, and hopefully not needed).
2. Break out of frames when you hit the page. This is probably the best solution if you can't control how the visitor arrives (by forwarding). You simply break out of frames and the frame (which you now seem to see as the main page, and it sure looks like that, but the browser doesn't see it like that) will be the main page... problem solved.

If you were to just forward them again, it wouldn't do anything because they would still be in a frame.

I hope this helps.

Giving a link to both of your sites would be very helpful if you have more questions, or if I misunderstood your initial question.

By the way, redirecting based on the referrer is possible (though somewhat unreliable), but it isn't too relevant in this situation (though the solution could involve checking that to see if you need to break out of frames or not).

01-19-2007, 06:21 PM
Thanks for the quick reply. I think I didn't do a very good job of explaining so I'll include the URLs so you can see what's happening. Old URL - www.garycurtisphoto.com, new URL www.gmcurtisphoto.com. Web.com tech support setup the auto re-direct. My old site doesn't exist anymore, it's just a domain name under an account my son has as a different (VPS) offering from web.com. If you read my note about the Verisign seal on my home page you should get a better idea of what's happening. Thanks again.

01-19-2007, 07:02 PM
Hmm.. different than I expected. Sorry about jumping to conclusions. It works just like you said... but no frames, as I thought made sense.
I'm really not sure what's going on.
Using PHP, you could try:
if ($_SERVER['HTTP_REFERER'] == 'http://this.com/page') {
Fairly easy.
Or if (strpos('site.com',$_SERVER['HTTP_REFERER']))
(If the url in question exists in the referer, not if it is exactly equal to)

Would PHP work for you?

01-19-2007, 08:08 PM
Sorry, but I'm a photographer with limited coding experience. If you could flesh out the code some more I could try it and see what happens, worst case if it doewn't work I remove the code and go back to where I'm at now. Thanks for your help.

01-19-2007, 11:06 PM
That IS the code. Obviously replace the URL with the right one.

PHP, however, is a bit hard in itself. Don't worry... not impossible.

First... it's a SERVER SIDE programming language. This means, in short, that, unlike javascript, which acts through the browser, the server outputs the script as just html. The browser never sees the PHP source. (<?php echo "<a>"; ?> would JUST output <a> into the source; that's it.)
This poses some weird issues, but, in short, it's nice, secure, and compatible with all systems* because it is parsed (interpreted/decoded/executed) by the server each time in the same way, not by the users' computers, which frequently vary.

The other piece about PHP is that, like with javascript when the browser much have javascript enabled, the server must have PHP both enabled and installed. Many servers do, though some don't. If you have admin access and wish to, you could install it.

Another simple thing, but a bit different is that PHP files (with an exception I get into because it involves modifying configuration files) MUST end with ".php", not ".htm", ".html", etc. Not to worry for you, though. It will act the same way, except for the filename. Browsers know that .php means, to them anyway, just another webpage.

Also, simlilar to javascript tags (kinda), PHP has special tags (as well as it's own syntax/language/commands within it). Each block you want to be interpreted as PHP must be within <?php ....... ?> tags. Fairly straight-forward.

PHP is very powerful, but, for this, you're just using a basic function.

What you need to do is:
1. Find the URL they came from... the "HTTP_REFERER" in PHP.
2. Check "IF" that meets the conditions you want
3. Then act on it, such as redirecting, etc.

Assuming there isn't something else unforseen that will affect this, the solution should be quite simple. Here's the PHP code, with comments. (Comments if you don't know are like sticky notes in a book you are reading... they aren't part of the text; they just help you understand it.) Feel free to remove the comments (anything like //this... at the end of a line) when you actually use the script (though they might be good to reference later).

<?php //open PHP er... mode... whatever you want to call it.
$lasturl = $_SERVER['HTTP_REFERER']; //set the variable $lasturl to the, well, last url.
//the array $_SERVER[... has lots of config/system/page info, like this, etc.
if (strpos('oldpage.com',$lasturl)) { //if 'oldpage.com' exists anywhere within the variable
//strpos returns the position of it. It will return FALSE if not found,
//so... that means it will just skip the if
//if it continues, we know that it's from the old page, because it had 'oldpage.com' in it.
//using 'oldpage.com' makes sense due to variations (like http://www. or just http://)
header("Location: http://mynewpage.com"); //send out http header data (this is how in PHP)
//and then set the Location to http://mynewpage.com
//note that it must be a FULL url, not a local page like "error.htm"
} //end the if
//below, end PHP
The header output MUST come before any text or the browser won't be able to handle it. The http data comes first, then text.

And here's an easier way of writing it, without comments, compressed for less space:
<?php if (strpos('oldpage.com',$_SERVER['HTTP_REFERER'])) { header("Location: http://mynewpage.com"); } ?>

(To simplify more, you could remove the braces - {} around the contents of the IF, but it's a good habit to leave them there. They are required if the IF lasts more than one line.)

I hope this is helpful.

Remember, before this will work you need what I mentioned above-- PHP installed/enabled, and the page to end with .php, etc.

01-20-2007, 12:26 PM
Thank you, I really appreciate all the work and effort you've put into helping me. I'm feeling a little overwhelmed by what you've presented but will see what I can do. I'll have to check with web.com tech support to see about some of the requirements and issues you mentioned. I'm using a template supplied by them that allows me to insert HTML and Javascript code but I not sure about using PHP. (this is totally foreign to me) I'll let you know if I can use it and if it works to solve my problem. Thanks again!