Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Getting the URL Minus the Query String

Getting the URL Minus the Query String

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasephpcomdebugginghelp
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Li Li Ken un
    wrote on last edited by
    #1

    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:

    1. 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"

    2. mysite.com/a/b/c?d=3&e=4

      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 named request. 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!

    P 1 Reply Last reply
    0
    • K Kevin Li Li Ken un

      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:

      1. 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"

      2. mysite.com/a/b/c?d=3&e=4

        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 named request. 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!

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      $_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, Peter

      Software rusts. Simon Stephenson, ca 1994.

      K 2 Replies Last reply
      0
      • P Peter_in_2780

        $_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, Peter

        Software rusts. Simon Stephenson, ca 1994.

        K Offline
        K Offline
        Kevin Li Li Ken un
        wrote on last edited by
        #3

        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 then explode 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!

        1 Reply Last reply
        0
        • P Peter_in_2780

          $_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, Peter

          Software rusts. Simon Stephenson, ca 1994.

          K Offline
          K Offline
          Kevin Li Li Ken un
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups