Forum Moderators: coopster

Message Too Old, No Replies

Return URL after mod rewrite

         

ladams02

4:09 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



I'm trying to retrieve the URL after the mod_write has taken place. I have a PHP poll and want to refresh the page (with the URL as it appears in the browser) to display the results. $SERVER['REQUEST_URI'] is returning the path to the include file, and not the address that appears in the browser.

Psychopsia

4:20 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



I use this function with $_SERVER['REQUEST_URI'] and works fine:

function requested_page()
{
$protocol = ((int) $_SERVER['SERVER_PORT'] === 443)? 'https://' : 'http://';
$current_page = $protocol . $_SERVER['HTTP_HOST'] . ((!empty($_SERVER['REQUEST_URI']))? $_SERVER['REQUEST_URI'] : '');

return $current_page;
}

ladams02

4:42 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



This worked on pages that aren't going through Apache's mod_rewrite. But for any pages that have the URLs rewritten, it returned the right-include.html file that the poll lives in, instead of the SEF URL of /something/somethingelse/yadda/ya

coopster

5:09 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sounds like you are doing an external redirect. Yes, that will cause the original address bar request to be replaced, meaning the REQUEST_URI to be the name of the new resource.

ladams02

5:23 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



coopster, can you explain a little further based on my situation:

The PHP poll lives in poll_include.php. This poll lives on the right rail of my page, right_rail_include.html. right_rail_include.html contains an include to poll_include.php.

Let's say the page that is calling this right_rail_include.html is index.php?u=username&many=otherparams&may=exist, however the mod_rewrite is accepting that index page as: /index/username/other/exist

So when I goto http://example.com/index/username/other/exist I see the poll living in the right rail. On this page, the VOTE or VIEW RESULTS links point to http://example.com/right_rail_include.html?poll=27&results=1#poll

I'm using $SERVER['URI_REQUEST'], is there any predefined PHP function to get the URL after the mod_rewrite has taken place, so I can refresh this page successfully?

MANY Thanks!

[edited by: coopster at 5:31 pm (utc) on Jan. 31, 2007]
[edit reason] generalized domain [/edit]

coopster

5:30 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




the VOTE or VIEW RESULTS links contain http://example.com/right_rail_include.html?poll=27&results=1#poll

If that is the published link then that is the link that is going to show up in the address bar and the resource at that link will be displayed in the browser, unless you are doing some form of external redirection.

Perhaps you can clarify a bit more what is going on ...
What does the link look like that you are clicking?
What do you expect to show in the address bar after clicking the link?

ladams02

5:44 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Sorry.. to clarify:

The page that I am on in the browser window displays: http://example.com/index/username/other/exist

So the link for View Results SHOULD point to (and be displayed in the browser window after page load):
http://example.com/index/username/other/?poll=27&results=1#poll

However, the ACTUAL link that is on the button (the bug) is pointing to is:
http://example.com/right_rail_include.html?poll=27&results=1#poll

So I'm assuming $_SERVER[URI_REQUEST] is grabbing this included file before the URLs have been rewritten. Is there a predefined PHP variable that will grab the URL from the browser window (after rewrite) and then I can add?poll=27&results=1#poll to the end.

jdMorgan

5:48 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to post the mod_rewrite code just to clarify this discussion...

Jim

ladams02

6:53 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Not sure how this will help, but here is an example of what the mod_rewrites are doing.

RewriteRule ^([^0-9]+/c/[^/]+)/?([0-9]*)/?$ /index.php?w=$1&p=$2 [L]

So it will allow users to goto:
[mydomain.com...]

and actually load the index.php page filling the "w" param.

coopster

7:04 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




However, the ACTUAL link that is on the button (the bug) is pointing to is:
http://example.com/right_rail_include.html?poll=27&results=1#poll

What is the exact PHP code you are using to build the link in this button? Can you paste it here? By the way, it is REQUEST_URI, not URI_REQUEST. I realize this may only be a typo here as you key into the forum discussion but it is going to be an issue if you are trying to use that in your actual PHP code.

ladams02

7:24 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



coopster, yes that was just a typo in the forum, good catch though. And actually I am using $_SERVER[PHP_SELF], but $_SERVER[REQUEST_URI] produces the same results.

In my poll include I am simply using <?= $_SERVER[PHP_SELF]?> to reference the page inside the link tag on the View Results button.

On static pages (where the URL is not being re-written) this works fine. But on any pages that are using mod_rewrite it grabs the location of the right_rail_include.html file.

coopster

7:29 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP_SELF and REQUEST_URI are two very different values. You should be able to build your SEF URI from the current resource if you use REQUEST_URI.

ladams02

7:42 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



PHP_SELF and REQUEST_URI both returned the right_rail_include.php URL, and not the SEF mod_rewrite version.

The reason I used PHP_SELF was because it dropped the params at the end of the URL, REQUEST_URI kept the original params on there (after page load), and when I added additional params to the end of it I had two sets of the same params on the URL. (sorry why am I typing so confusing today?)

But both generated the same URL or path, that is the right_rail_include.php. Any other ideas/methods to try and get the exact address that is displayed in the browser's address bar?

whoisgregg

8:14 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the poll included with an iframe? If so, REQUEST_URI from within the poll script will report the URI of the iframe, not of the page that calls the iframe.

ladams02

8:17 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



No iframes are being used.

ladams02

8:18 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



I was hoping someone have might of come across this previously and could offer some quick advice, but it looks like this might be something I will have to toy around with for a while. Any other bits of info would be appreciated, otherwise I will keep you posted. Thanks all!

coopster

8:23 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What do your includes look like? Are you using the filesystem socket or the HTTP protocol to include your files?

whoisgregg

9:19 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason I suspected an iframe is because of the poll being included with a query string. I think you nailed it coopster with the HTTP protocol for includes.

This doesn't work because local filepaths don't use query strings:

include('/path/to/local/file.php?query=string&is=not&allowed=here');

This does work if url wrappers [us2.php.net] are enabled:

include('http://example.com/file.php?query=string&is=not&allowed=here');

What happens though is the file.php that is being 'included' is actually being requested and fetched from your website so it's REQUEST_URI is the one with the query string. To address this, just call includes that expect $_GET parameters like so:

$_GET['query'] = 'string';
$_GET['is'] = 'definitely';
$_GET['allowed'] = 'here';
include('/path/to/local/file.php');

Just be careful to avoid overwriting $_GET values that are in use on your main script. :)