Forum Moderators: coopster

Message Too Old, No Replies

Forcing the user off my domain when redirecting them

         

ocon

1:53 am on Apr 14, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



If I have a file at example1.com/index.php that says:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http:// www.example2.com"); //Space before the www is so the link doesn't become live
?>

Then the user accessing example1.com would be automatically taken to example2.com and their address bar would be changed to reflect this.

However, if instead of example2.com, the script used example3.com, with example3.com being a non-existing domain.

Then when the user accesses example1.com, the browser would fail ("Server not found" in FF), which is to be expected since example3.com doesn't exist. The only problem is, the address bar would still show example1.com (which does exist), not example3.com.

Is there anyway to force the user off the example1.com domain, and onto the example3.com domain?

I have several scripts on my sites that redirect users. One example is when I have exampel.com redirecting to example.com. If the user tries to access exampel.com/non-existent-folder/ I want the user to fail on the proper domain: example.com/non-existent-folder/

Also, I have other link tracking scripts, such as example.com/link-tracker.php?url=http://www.nonexistantdomain.com. I would want this user to be moved to nonexistentdomain.com when they get their "Server not found" error.

CyBerAliEn

3:48 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



I believe this is a browser "issue", not a PHP one. I imagine there is a specification that browsers follow that the address doesn't change if it cannot establish a connection in this circumstance... PHP simply sends a header request to the browser, telling it to connect to some other place. Apparently, if this place doesn't exist... you get the appropriate 404 error, but you are right that the address bar does not change.

You CAN directly manipulate the address bar of the browser though via Javascript (not PHP). Let's suppose you have a user request:
http://www.example.com/goes-somewhere/

And let's say this location on example.com is suppose to send the user over to newexample.com, ok? But if the page on newexample.com doesn't exist, you get a 404 error. The server (for newexample.com) will say "this doesn't exist", and return a 404 error (usually by default). Your browser will then display its own "404 error happened" page. [usually]

What you can consider doing is this:

Configure your server (for newexample.com) to relay 404 errors to a specific page, say:
http://www.newexample.com/error404.php
(sometimes these files are placed above the web root)

You can easily do this if you are using site admin software like cPanel (I think cPanel has a section called "custom error pages" or such for this); otherwise do a search on how to do it.

If a user requests a page on newexample.com that doesn't exist (via a redirect from example.com)... the server at newexample.com should display the 404 error page from newexample.com.... resulting in the address bar changing. The server will return the page content (which should say something like "Page not found"), the address bar will change, and the server will relay a 404 error header to the browser, notifying the browser (internally) that the requested page doesn't exist.

The issue with this, is that if you have no control over newexample.com --- there isn't anything you can do with just PHP! If you own/have example.com and are redirecting to newexample.com --- but newexample.com is 3rd party, you got some issues.

The only way to redirect from your site, to the 3rd party site, and ensure the address bar changes, is to utilize Javascript, specifically:
<script type="text/javascript">window.location = 'http://www.newexample.com';</script>


This will force the user's browser window (and hence address bar) to change to the noted URL, forcing a redirect. You could combine this JS with PHP on your own server to enhance it. For example, if you have "link-tracker.php?url=somewhere.com"... you could change the code of the PHP file so that PHP gets the go-to-URL, and routes it to JS output that then redirects the browser.

Problems with this alternate method, is that you are now using JS to route the user. If a user has JS disabled (uncommon), they won't get redirected; so you have to account for this (add a note and a link in a NOSCRIPT tag). If you are concerned about "link juice", this method may not be something you want to consider.

But you could get creative... you could use some PHP functions to try and "get" the URL. If the URL doesn't exist, the function returns boolean false (I think you can use get_file_contents() or such to retrieve URLs). So.... if a request URL exists, use normal PHP redirection. If your PHP function says "hey, page not exist!", then echo out some JS to do the redirect... the user will still see "no page found" (404) error, but then the address bar will change.

Readie

3:50 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A .htaccess 301 redirect *might* work better than a PHP one.

CyBerAliEn

4:05 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



^A good alternative; but both perform the same behavior: sending a header request to the browser to get the 'other page'. They both perform the same process, one just uses PHP to do it, the other uses Apache to do it.

Clearly, the problem is in the browser behavior. Just a header request to a bad page means a 404 from the bad page's server, but the address bar in the browser doesn't update. I'm actually a little perplexed why this IS the behavior.

I didn't quite believe this is the behavior, but a few tests on my own server verify it. I suppose there is a valid reason for it I am not aware of.

Readie

4:52 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps to prevent a bad page being added to the browser history/cache?

ocon

6:30 pm on Apr 14, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you very much for your through replies.

In some situations I do have control over the domain, so I will institute a 404 redirect. I just don't know how that's going to play out for when someone tries going to non-existant-subdomain.existing-domain.com.

For other situations, instead of outputing a PHP redirect, I'll output html with a javascript redirect, a meta html redirect, as well as an html link.

Thanks