Getting the URL Minus the Query String
-
At the moment, I have index.php located at the root. I want to have it parse all my URLs and delegate the workload to the correct code (via URL rewriting). I may be a bit nit-picky, but I'd like to know if there's an elegant solution to neatly separating the request URL and the key-value pairs of the query? My index.php looks like this:
If I give it some URLs, they produce the corresponding outputs:
-
mysite.com/a/b/c?d=1&e=2&request=3
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=1&e=2&request=3"
Request ($_GET["request"]): "3"
d="1"
e="2" -
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=3&e=4"
Request ($_GET["request"]): "a/b/c"
d="3"
e="4"
$_SERVER["REQUEST_URI"]
includes everything after mysite.com including the raw query string, and$_GET["request"]
will not return the requested URL minus the query string if there is a key namedrequest
. I could parse the value of$_SERVER["REQUEST_URI"]
myself, but I'd like to use built-in functionality before coding around the problem.
My GUID: ca2262a7-0026-4830-a0b3-fe5d66c4eb1d :) Now I can Google this value and find all my Code Project posts!
-
-
At the moment, I have index.php located at the root. I want to have it parse all my URLs and delegate the workload to the correct code (via URL rewriting). I may be a bit nit-picky, but I'd like to know if there's an elegant solution to neatly separating the request URL and the key-value pairs of the query? My index.php looks like this:
If I give it some URLs, they produce the corresponding outputs:
-
mysite.com/a/b/c?d=1&e=2&request=3
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=1&e=2&request=3"
Request ($_GET["request"]): "3"
d="1"
e="2" -
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=3&e=4"
Request ($_GET["request"]): "a/b/c"
d="3"
e="4"
$_SERVER["REQUEST_URI"]
includes everything after mysite.com including the raw query string, and$_GET["request"]
will not return the requested URL minus the query string if there is a key namedrequest
. I could parse the value of$_SERVER["REQUEST_URI"]
myself, but I'd like to use built-in functionality before coding around the problem.
My GUID: ca2262a7-0026-4830-a0b3-fe5d66c4eb1d :) Now I can Google this value and find all my Code Project posts!
$_SERVER["QUERY_STRING"]
will give you the query string. (I forget if it includes the ? or not.) Picking off everything prior to the first ? (i.e. the URL part) isn't too hard. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
-
-
$_SERVER["QUERY_STRING"]
will give you the query string. (I forget if it includes the ? or not.) Picking off everything prior to the first ? (i.e. the URL part) isn't too hard. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
Interestingly enough, I get all the pieces of the request neatly delimited by ampersands. mysite.com/a/b/c?d=1&e=2&request=3
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=1&e=2&request=3"
Request ($_GET["request"]): "3"
Request ($_SERVER["QUERY_STRING"]): "request=index.php&request=a/b/c&d=1&e=2&request=3"
d="1"
e="2"mysite.com/a/b/c?d=1&e=2&request=%26+%3F
Debug Output
Request ($_SERVER["REQUEST_URI"]): "/a/b/c?d=1&e=2&request=%26+%3F"
Request ($_GET["request"]): "& ?"
Request ($_SERVER["QUERY_STRING"]): "request=index.php&request=a/b/c&d=1&e=2&request=%26+%3F"
d="1"
e="2"Although the key-value pairs remain encoded, I guess it's still better to just use
explode
to get the key-value pairs first and thenexplode
the resulting items to get the keys and values.
My GUID: ca2262a7-0026-4830-a0b3-fe5d66c4eb1d :) Now I can Google this value and find all my Code Project posts!
-
$_SERVER["QUERY_STRING"]
will give you the query string. (I forget if it includes the ? or not.) Picking off everything prior to the first ? (i.e. the URL part) isn't too hard. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
I did some testing, and it appears that certain values in the query can replace
$1
in .htaccess with just 406.shtml or 406_shtml. mysite.com/a/b/c?d=1&e=2&� will cause this problem. It looks like I will have to parse the contents of$_SERVER['REQUEST_URI']
within the script if I want to handle all the cases without fail.
My GUID: ca2262a7-0026-4830-a0b3-fe5d66c4eb1d :) Now I can Google this value and find all my Code Project posts!