RewriteRule to another domain problem

I know nothing about programming or running a server, but I’ve had my server for about 10 years and over time I’ve added this and that to different code. Below is a code I have used to protect access to a site. Please note that below the SPACE after “http://” isn’t really in my code, I just put it here to prevent a link from showing up

RewriteEngine On
RewriteCond %{HTTP_referer} !^http:// (www.)?MAINSITE\.com/members/ [NC]
RewriteCond %{HTTP_referer} !^http:// (www.)?DAUGHTERSITE\.com/members/ [NC]
RewriteRule .* http:// www.MAINSITE.com/members/term.php?Term=abc1\&Site=daught\ site\&page=members [R=301,L]

What I want to do with this is allow access to DAUGHTERSITE/members from MAINSITE and only from MAINSITE

If someone tries to access DAUGHTERSITE/members from anywhere, they are redirected to http:// www.MAINSITE.com/members/term.php?Term=abc1\&Site=daughter\ site\&page=members which a link is presented to take them to DAUGHTERSITE/members

I thought this worked for a few years, but now I’m noticing that I sometimes get an error. Don’t really know if these errors were there all along, but kind of think so. Much of the time things work as planned, but sometimes when I click the last link to goto DAUGHTERSITE/members I remain on the exact same page without going anywhere–it’ll start to do something like it’s about to load, then I see the same URL of the page I am already on load and I’m on the same page without moving.

This doesn’t happen all the time, but once it does happen, it seems to happen continually for awhile if I use the same browser. I sometimes get different results with another browser or another computer.

I’m wondering if there might be an issue in the code which makes this work much of the time, but other times the server just says…“ummmm, nope, not this time!”

What I’ve tried… I’ve tried adding Options +FollowSymlinks but doesn’t seem to make any difference. I even tried removing the “=301” but that also doesn’t seem to matter.

Using Cpanel. Both sites are mine and on the same server. The htaccess is located at DAUTHERSITE/members/ Any suggestions much appreciated. I wrote this stuff piece by piece over the years from stuff I read and copied parts of, but I now have no clue what any of it does when I look at it.

It says:

If the visitor does not come from [noparse]www.MAINSITE.com/members[/noparse] or MAINSITE.com/members, or [noparse]www.DAUGHTERSITE.com/members[/noparse], or DAUGHTERSITE.com/members or any case variant of the aforementioned (like [noparse]wWw.MaInSiTe.coM/MemBerS[/noparse]), do a 301 (permanent, i.e., browsers are allowed to cache) redirect to [noparse]www.MAINSITE.com/members/yadie/yada[/noparse]

Looks fine to me :slight_smile:
Could you try to reproduce when it doesn’t work? Without a real test case there isn’t much we could do I’m afraid.

Also, try installing Live HTTP Headers for firefox to see the headers come by for a closer inspection of what’s going on :slight_smile:

Thanks, that helped me solve it. I installed that extension and that proved useful. Ultimately, what I did was add header('Referer: '.$_SERVER[‘PHP_SELF’]); to the page 2 past MAINSITE.com/members and it seems to work fine now. I don’t know what everything does but I just try everything possible until I stumble upon something and I found someone talking about that at this forum. The page immediately after MAINSITE.com/members is a redirect to DAUGHTERSITE/members which has the Rewrite code listed previously and seems people were getting bounced back to MAINSITE.com/members (not exactly just staying on MAINSITE.com/members as it might seem as the server was sending them through the redirect page and then starting to send to MAINSITE.com/members but then sent back to the location listed in the Rewrite.

I’m going to do some research into more exactly what header('Referer: '.$_SERVER[‘PHP_SELF’]); is and does. I have a very basic idea which is why I thought to try it.

Below is exactly what I added 2 pages past MAINSITE/members. This is the page I want the members to get to.

<?php
header('Referer: '.$_SERVER[‘PHP_SELF’]);

$memberPage = “on”;
chdir(“…/”); include(“home.php”);

?>

Just to be safe, I also added on the page immediately after MAINSITE/members. This page just counts the member and lets me know where they are going.

<?php
header('Referer: '.$_SERVER[‘PHP_SELF’]);
$page = “members”;

$con = mysql_connect(“localhost”,“xyz”,“topsecretcode”);
mysql_select_db (“xyz_com_-_site-list”);

$ID = $_GET[‘ID’];
$result = mysql_query(“UPDATE Sites SET Clicks=Clicks+1 WHERE ID = ‘$ID’”)or die(mysql_error());;
$result = mysql_query(“SELECT * FROM Sites WHERE ID = ‘$ID’”)or die(mysql_error());;

$row = @mysql_fetch_array($result);

header(“Location:” .$row[‘Members Access’]); /* Redirecting to DAUGHTERSITE/members */

exit;
?>

Adding the header('Referer: '.$_SERVER[‘PHP_SELF’]); seemed too solve everything. Strange that without it, the pages seemed to have worked for years. I am always working on the sites and making small changes but can’t think of anything too radical I’ve done recently that would have caused more of a problem, and infact more times than not the code seemed to do exactly what I expected.