how to read url

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
unisabilly
New php-forum User
New php-forum User
Posts: 19
Joined: Wed Aug 17, 2005 6:00 am

Sat Jan 21, 2006 9:40 am

is there any way that i can read the actual url entered by the user !?
ie. not $_SERVER["SCRIPT_NAME"] nor $_SERVER["PHP_SELF"] coz those two give the excuted page instead of the actual url entered by the user, what i am looking for is something like var url = document.write(location) of javascript.
the reason i need the url entered by the user is that i'm going to parse the url and redirect the user to the best match page if the page that user entered does not exist ie.under 404.shtml i'll use <!--#include file="404.php"--> therefore if i use $_SERVER["SCRIPT_NAME"] or $_SERVER["PHP_SELF"] it will return 404.php instead of the url entered by the user.

for example
if user enter the following: http://www.mydomain.com/monitor (which the directory does not even exist)
the 404.shtml page will redirect to : http://www.mydomain.com/search.php?word=monitor

another example
if user enter the following: http://www.mydomain.com/ibm
redirect to : http://www.mydomain.com/shop.php?id=shop00001 (search the shop id from the database first, if exist redirect to the page of the shop dirctly, else redirect to the search page like first example)

User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Sat Jan 21, 2006 10:06 pm

unisabilly wrote:is there any way that i can read the actual url entered by the user !?
ie. not $_SERVER["SCRIPT_NAME"] nor $_SERVER["PHP_SELF"] coz those two give the excuted page instead of the actual url entered by the user, what i am looking for is something like var url = document.write(location) of javascript.
the reason i need the url entered by the user is that i'm going to parse the url and redirect the user to the best match page if the page that user entered does not exist ie.under 404.shtml i'll use <!--#include file="404.php"--> therefore if i use $_SERVER["SCRIPT_NAME"] or $_SERVER["PHP_SELF"] it will return 404.php instead of the url entered by the user.

for example
if user enter the following: http://www.mydomain.com/monitor (which the directory does not even exist)
the 404.shtml page will redirect to : http://www.mydomain.com/search.php?word=monitor

another example
if user enter the following: http://www.mydomain.com/ibm
redirect to : http://www.mydomain.com/shop.php?id=shop00001 (search the shop id from the database first, if exist redirect to the page of the shop dirctly, else redirect to the search page like first example)
Why? It's long and I'm sure what better use a trap of the request.
Each server have use error's pager is ready. All what to you need make a trap a request. In apache it's very simple: see conf file and find Error handler. see.... put instead this file a redirect with parametr.... what's all...
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image

User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Sun Jan 22, 2006 5:24 pm

Try this out:

Code: Select all

<?php print $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; ?>
Sincerely,
Gonçalo "gesf" Fontoura

gesf.org | sessionstart.com | urlms.com

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2213
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Mon Jan 23, 2006 11:52 am

gesf surely forgot a "?" before the query_string

however for the what you want mod_rewire might be best.. you might be able to simply get all the requests there and with help of some logic: redirect to the search engine..
Best Regards,
Alexej Kubarev
-------------------------------
Zend Certified Engineer
Image Image

User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Tue Jan 24, 2006 2:51 am

:( Buááááhhhhhhhhhhh !!!
Sincerely,
Gonçalo "gesf" Fontoura

gesf.org | sessionstart.com | urlms.com

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1279
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Tue Jan 24, 2006 8:13 pm

yes

Code: Select all

$_SERVER['REQUEST_URI']
works even when you rewrite your page for a 404 ErrorDocument in Apache

User avatar
Shiv
php-forum Active User
php-forum Active User
Posts: 262
Joined: Wed Feb 02, 2005 8:06 pm
Location: Ahmedabad (Gujarat), INDIA

Tue Jan 24, 2006 8:43 pm

Hey, $_SERVER['REQUEST_URI'] doesnt working on IIS. I dont know why it is not working, can anybody tell me?
I use $_SERVER['HTTP_REFERER'] works in both.

User avatar
Shiv
php-forum Active User
php-forum Active User
Posts: 262
Joined: Wed Feb 02, 2005 8:06 pm
Location: Ahmedabad (Gujarat), INDIA

Thu Jan 26, 2006 9:09 pm

viralupadhyaya wrote:Hey, $_SERVER['REQUEST_URI'] doesnt working on IIS. I dont know why it is not working, can anybody tell me?
I use $_SERVER['HTTP_REFERER'] works in both.
Hellooo........ :(

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2213
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Fri Jan 27, 2006 1:50 am

okej..well this might be a IIS bug or that it doesnt support it...
Best Regards,
Alexej Kubarev
-------------------------------
Zend Certified Engineer
Image Image

unisabilly
New php-forum User
New php-forum User
Posts: 19
Joined: Wed Aug 17, 2005 6:00 am

Fri Jan 27, 2006 2:37 am

viralupadhyaya wrote:Hey, $_SERVER['REQUEST_URI'] doesnt working on IIS. I dont know why it is not working, can anybody tell me?
I use $_SERVER['HTTP_REFERER'] works in both.
try the following code, it should display all the variables in $_SERVER

Code: Select all

<?php
  foreach ($_SERVER as $key => $value)
	echo '$_SERVER["'.$key.'"] = '.$value."<br />"; 
?>
Alexei Kubarev wrote:gesf surely forgot a "?" before the query_string

however for the what you want mod_rewire might be best.. you might be able to simply get all the requests there and with help of some logic: redirect to the search engine..
I've been search for the articles on mod rewrite but i still have no clue how it works

here is my first try :
Q. redirect the following address (which does not exist) http://www.mydomain.com/doodie
to http://www.mydomain.com/search/search.p ... ord=doodie

Code: Select all

RewriteRule ^([a-zA-Z0-9\._-]+)/?$    /search/search.php?display=search&word=$1
no suprise it doesnt work ...
i know the code above might look stupid to you, but ... I tried my best ~ please help ~>_<~
Last edited by unisabilly on Fri Jan 27, 2006 3:29 am, edited 1 time in total.

User avatar
Shiv
php-forum Active User
php-forum Active User
Posts: 262
Joined: Wed Feb 02, 2005 8:06 pm
Location: Ahmedabad (Gujarat), INDIA

Fri Jan 27, 2006 2:57 am

Alexei Kubarev wrote:okej..well this might be a IIS bug or that it doesnt support it...
ThanX Alexei :)

User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Sat Jan 28, 2006 11:45 pm

Well, not sure if it's a bug or not but $_SERVER['REQUEST_URI'] doesn't work in IIS.
You my combine $_SERVER['SCRIPT_NAME'] plus ? plus $_SERVER['QUERY_STRING'].

For unisabilly. That may work, but i suggest to use some variable to assign the search string.
Try this example:

Code: Select all

RewriteEngine On
RewriteBase /
RewriteRule ^search/$    /search/search.php [L]
RewriteRule ^search?q=(.*)$    /search/search.php?display=search&word=$1 [L]



# So: http://www.mydomain.com/search?q=doodie 
# Will take you to: http://www.mydomain.com/search/search.php?display=search&word=doodie 
# And: http://www.mydomain.com/search
# Will take you to: http://www.mydomain.com/search/search.php
Sincerely,
Gonçalo "gesf" Fontoura

gesf.org | sessionstart.com | urlms.com

Post Reply