hide forwarded url in address bar

Discuss practical ways rearrange URLs using mod_rewrite.

hide forwarded url in address bar

Postby madcrazy1 » Fri Sep 28, 2007 7:01 pm

Hi,
Iwas referred here by phpfreaks, they said it can be done with .htaccess.
I have a php script that redirects the url input into the address bar by a surfer
user types in address bar:
mysite.com/usersite
user gets to user site but the address bar now shows the redirect url
mysite.com/redir?usersiteid=389
this is part of the existing code:


Code: Select all
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/redir?usersite=$usersiteid");
}
else
{
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/infoseek.php");[ /code]

what i want to happen is:
the address bar keeps what the user types in and not the redirect url
help please


madcrazy1
 
Posts: 7
Joined: Fri Sep 28, 2007 6:56 pm

Postby richardk » Sat Sep 29, 2007 8:08 am

Iwas referred here by phpfreaks

I'm not sure why they did.

I have a php script that redirects the url input into the address bar by a surfer
user types in address bar:
mysite.com/usersite
user gets to user site but the address bar now shows the redirect url
mysite.com/redir?usersiteid=389

Do you already have mod_rewrite to do that?

Try replacing
Code: Select all
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/redir?usersite=$usersiteid");

with
Code: Select all
$_GET['usersite'] = $usersiteid;
include(getenv('DOCUMENT_ROOT') . '/redir');
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Ok

Postby madcrazy1 » Sat Sep 29, 2007 11:26 am

That seemed to keep the url that was typed originally in the address bar
but gave the page not found error because internally it was not redirected
to the proper place.

this is how it should redirect internally (behind the scenes, so to speak)
user types url like;
home.com/myplace
user gets redirected to
script?usersite=userid
after redirecting to the script address bar should still show
home.com/myplace

right now with the mod you gave me, it stays at
home.com/myplace and sits there. it is not redirecting to the actual script
here is the whole thing if it helps:
Code: Select all
$requested = ereg_replace("/", '', $_SERVER['REQUEST_URI']);
$RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
            $RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
            $result = $connector->query($RunThisQuery);   
            while ( $row = $connector->fetchArray($result) ) 
               {
                        $membernumber= $row['userid'];
                        $redir= blog.php?id
            }
   $_GET['id'] = $membernumber;
include(getenv('DOCUMENT_ROOT') . '/$redir');
}
else
{
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/browse.php");
}



thanks again
madcrazy1
 
Posts: 7
Joined: Fri Sep 28, 2007 6:56 pm

Postby richardk » Sat Sep 29, 2007 12:31 pm

Try
Code: Select all
$RunThisQuery = 'SELECT username,userid FROM frm_users WHERE username="' . str_replace('/', '', getenv('REQUEST_URI')) . '"';
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
  while($row = $connector->fetchArray($result))
  {
    $_GET['id'] = $row['userid'];
  }
  include(getenv('DOCUMENT_ROOT') . '/blog.php');
}
else
{
  header('Location: http://' . getenv('HTTP_HOST') . '/browse.php', true, 301);
}


If it doesn't work, replace
Code: Select all
getenv('DOCUMENT_ROOT') . '/blog.php'

with the full physical server path to blog.php.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Sorry, i pasted the wrong code it is:

Postby madcrazy1 » Sat Sep 29, 2007 6:18 pm

Code: Select all
$requested = ereg_replace("/", '', $_SERVER['REQUEST_URI']);
$RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
            $RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
            $result = $connector->query($RunThisQuery);   
            while ( $row = $connector->fetchArray($result) ) 
               {
                        $membernumber= $row['userid'];
            }
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/blog.php?id=$membernumber");
}
else
{
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/browse.php");
}
?>

Code: Select all
include(getenv('DOCUMENT_ROOT') . '/blog.php');
In addition to the wrong code i gave you , sorry! i am not sure how to assemble an alternate path
like this?
include(var/www/html/site/blog.php);

thanks again
madcrazy1
 
Posts: 7
Joined: Fri Sep 28, 2007 6:56 pm

Postby richardk » Sun Sep 30, 2007 12:03 pm

The PHP i provided should still work.

i am not sure how to assemble an alternate path
like this?

You only need to change it if you get include() errors. It needs 's around it and to start with a /
Code: Select all
include('/var/www/html/site/blog.php');
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Ok I tried this:

Postby madcrazy1 » Sun Sep 30, 2007 12:43 pm

Code: Select all
$requested = ereg_replace("/", '', $_SERVER['REQUEST_URI']);
$RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
            $RunThisQuery = "SELECT username, userid from frm_users where username="' . str_replace('/', '', getenv('REQUEST_URI')) . '"';
            $result = $connector->query($RunThisQuery);   
            while ( $row = $connector->fetchArray($result) ) 
               {
                        $membernumber= $row['userid'];
            }
   include('/var/www/html/biz/blog.php?id=$membernumber');
}
else
{
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/browse.php");
}
?>


I think i put together everything you intended
but now i get
This error (HTTP 404 Not Found) means that Internet Explorer was able to connect to the website, but the page you wanted was not found. It's possible that the webpage is temporarily unavailable. Alternatively, the website might have changed or removed the webpage.

For more information about HTTP errors, see Help.
thank you
madcrazy1
 
Posts: 7
Joined: Fri Sep 28, 2007 6:56 pm

Postby richardk » Sun Sep 30, 2007 1:04 pm

Code: Select all
include('/var/www/html/biz/blog.php?id=$membernumber');

You can not use a query string with include().

Use the code i provided
Code: Select all
$RunThisQuery = 'SELECT username,userid FROM frm_users WHERE username="' . str_replace('/', '', getenv('REQUEST_URI')) . '"';
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
  while($row = $connector->fetchArray($result))
  {
    $_GET['id'] = $row['userid'];
  }
  include('/var/www/html/biz/blog.php');
}
else
{
  header('Location: http://' . getenv('HTTP_HOST') . '/browse.php', true, 301);
}


If that does not work, try
Code: Select all
$requested = ereg_replace("/", '', $_SERVER['REQUEST_URI']);
$RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
$result = $connector->query($RunThisQuery);

if ($connector->fetchNumResult($result) > 0)
{
            $RunThisQuery = "SELECT username, userid from frm_users where username = '$requested'";
            $result = $connector->query($RunThisQuery);   
            while ( $row = $connector->fetchArray($result) )
               {
                        $_GET['id'] = $row['userid'];
            }
  include('/var/www/html/biz/blog.php');
}
else
{
   header("Location: http://" . $_SERVER['HTTP_HOST'] . "/browse.php");
}

If you get an error, copy and paste it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

tried it but...

Postby madcrazy1 » Sun Sep 30, 2007 3:58 pm

I copied and pasted the above code (both versions), anyhow they both redirect to browse.php now. which is not the desired result
I can't figure out why you have this line in there:
Code: Select all
$_GET['id'] = $row['userid'];

is this the problem?

you seem to have $_GET['id'] defined but i dont see it being used anywhere
madcrazy1
 
Posts: 7
Joined: Fri Sep 28, 2007 6:56 pm

Postby richardk » Mon Oct 01, 2007 11:08 am

It's used in blog.php. It's like doing "?id=abc", as that wouldn't work with file system include()s.

anyhow they both redirect to browse.php now

That sounds like the SQL query isn't returning any results or that the redirect has been cached by your browser. Try clearing your browser's cache and using the second piece of code.

You can also try replacing
Code: Select all
$_GET['id'] = $row['userid'];

with
Code: Select all
$_GET['id'] = $_REQUEST['id'] = $id = $row['userid'];

in case blog.php doesn't use $_GET['id'].

Plus, this isn't really my area, it's PHP. All that script needs to do is set the id variable so blog.php can use it and include() blog.php, so you could ask at a PHP forum or the script author.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 52 guests

cron