REQUEST_URI without Query String

How to use:


without getting the query string, just getting the page directory or file. I’m not talking about PHP_SELF because that doesn’t work if the URL needs to be displayed as:

and NOT:

unless directories aren’t used like:

that would be okay, but it needs to be suitable for either scenario, showing either the directory or page, but not the directory AND page, and definitely NO query string.

Thanks, Bryan

What about
Are you using mod_rewrite?
What url are you actually having problems with? If you want to ignore index.* then you will have to make it a special case.
Do any of the request parameters actually have an effect on the page?

It would be oh so simple if it were just a single page, but the purpose is to dynamically and cleanly collect ANY URL, but it’s not meant to actually rewrite the page/browser URL.

Here’s the full code I’m using:

http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '

and in many cases is very good about presenting a clean URL to display already, even removes # and everything after, but if there’s a query ? or & it goes on ahead and adds it anyways, I know that’s how REQUEST_URI is programmed to work, but I just find it awfully annoying and out of place that it does that because there’s already REQUEST_QUERY or something similar for that purpose.

So I’ve been wracking my brain the last few days trying to kill the query string from being displayed with the URL, again this isn’t a .htaccess thing because I don’t want to manipulate the actual page URL, but collect it and clean it for other display purposes. It always seems like the simplest task is always the hardest one.

I know there must be a way, there is always a way.

Thanks, Bryan

I think what you want isn’t possible, ie “ANY URL”

The values will be different depending on whether mod_rewrite is being used or whether mod_rewrite is used with a query string as well.

Also there is no generic way to know what the meaningful URI is. I might have or, where view=4 is important, track=anything isn’t.

Basically, you need to have some schema with which to match/build your canon thingos against.

str_replace( $_SERVER['REQUEST_QUERY'], '', $_SERVER['REQUEST_URI'] );


It is definitely possible. In fact I already know how to do it in .htaccess:

RewriteCond %{REQUEST_URI} !^/search-results/
RewriteCond %{QUERY_STRING} !=“”
RewriteRule ^(.*)$ /$1? [R=301,L]

with the top line being for adding exceptions, pages you don’t want to remove query strings from, but again, we’re not talking about .htaccess or rewriting the actual page URL.


You’re definitely on the right track my friend, but maybe you can help me with the syntax. Because, the context in which I need the code presented throw errors when using ():

function clink() {
echo '<link rel="canonical" href="http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '" />';

Thanks, Bryan

  string(15) "params=test/a/b"
  string(9) "/test/a/b"

That’ll work a treat :slight_smile:

@calmest, just look at your server vars, what in particular are you having problems with? print the strings out for us.

There is no specific problem, I just need to create clean URLs dynamically, dynamic being the key word. I simply need to strip queries off URLs, there must be a way in PHP to say if ? is found, remove it and everything after it…

There’s no other way I can say it or put it, I’m sure there’s someone that’s run into this before and has an answer, no worries though, I don’t want anyone to put too much time into this, if you have the answer superb, if not please don’t waste any more time on it.

Thanks, Bryan

Logic_earth has your answer, I am/was confused as mod_rewrite can screw with your query string. With plain file access and a query string, you get this:

  string(3) "a=b"
  string(21) "/content/test.php?a=b"
  string(17) "/content/test.php"
  string(17) "/content/test.php"

I’m not sure why SELF or SCRIPT isn’t enough, but you can str_replace the query in the uri and trim the ?

Don’t worry about people wasting time, it’s theirs to waste :slight_smile: I just think you need to post some concrete examples.

Edit, I’m also assuming you want to do this for canonicial URLs, rather than pretty URLs? That is, you want to remove unnecessary tracking etc from a url to prevent duplicates rather than just make it look nice.

I think logic_earth is on the right track too, maybe they’ll help me with it more…

The reason SELF doesn’t work is because you would NEVER want, you want

Thanks, Bryan

As mentioned earlier, that is a special case. Just trim index.* (whatever is in your httpd.conf) off.