3

I'm setting up a redirect URL where pass REQUEST_URI from .htaccess to PHP file.

Here is my files structure:

  • index.php
  • .htaccess
  • folder/index.php

If the URL is http://example.com/?something

I want to pass %{REQUEST_URI} full URL from .htaccess to folder/index.php and retrieve it with $_SERVER['REQUEST_URI'].

My folder/index.php:

echo $_SERVER['REQUEST_URI'];

.htaccess:

UPDATED:

Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteCond %{REQUEST_URI} !/folder/index.php$
    RewriteRule .* /folder/index.php [L,NE]
</IfModule>

I expect to echo REQUEST_URI in PHP file but always give external server.

DocRoot
  • 1,176
  • 10
  • 17
Robert
  • 59
  • 1
  • 9
  • 1
    What do you mean by "it always give **external server** ? – Amit Verma Dec 21 '18 at 01:10
  • when type mydomain.com/?something it gives 500 external server page instead of passing the REQUEST_URI from htaccess to php file. – Robert Dec 21 '18 at 09:16
  • Usually you don't need .htaccess to get that variable value. `$_SERVER["REQUEST_URI"]` gets it. Just place the script containing that instruction in _/public_html_ or similar directory – Felipe Alameda A Dec 23 '18 at 10:18

2 Answers2

3

I want to pass %{REQUEST_URI} full URL from .htaccess to folder/index.php and retrieve it with $_SERVER['REQUEST_URI'].

You can't actually do this (but I don't think this is what you are trying to do - or even want to do - anyway). To clarify...

The PHP superglobal $_SERVER['REQUEST_URI'] is populated by PHP and this cannot be overridden.

Whilst %{REQUEST_URI} (the Apache server variable) and $_SERVER['REQUEST_URI'] (the PHP superglobal) are similar, they reference different values:

  • %{REQUEST_URI} (Apache server variable) - references the URL-path only (no query string). The value is %-decoded. If the request is rewritten, then this is updated to contain the rewritten URL, not the URL of the initial request.

  • $_SERVER['REQUEST_URI'] (PHP superglobal) - Holds the initial URL-path and query string the user requested (not the rewritten URL). The value is not %-decoded.

If you really do want to reference the Apache server variable REQUEST_URI in PHP then you need to explicitly pass it (perhaps as an environment variable or query string) - but depending on when you "pass it", you could end up passing different values. And either way, this will not change the value of $_SERVER['REQUEST_URI'], which, as mentioned above, is controlled by PHP.

For example:

RewriteRule ^ - [E=APACHE_REQUEST_URI:%{REQUEST_URI}]

And reference this in PHP as getenv('APACHE_REQUEST_URI').


UPDATE:

By the sounds of it, you probably want something like the following instead:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(index\.php)?$ folder/index.php [L,NE]

This internally rewrites any request for the document root, that contains a query string (everything after the ?) to /folder/index.php. When the query string is absent then index.php in the document root is served as normal.

Then, in /folder/index.php you simply access $_SERVER['QUERY_STRING'] to access the query string (the string to redirect to).

DocRoot
  • 1,176
  • 10
  • 17
  • "i tried your code but does not work" - the code I posted _does_ work, but it isn't meant to solve your specific problem. This sounds like an [XY problem](https://en.wikipedia.org/wiki/XY_problem)? What is it you are actually trying to do? "I want to pass the full `$_SERVER['REQUEST_URI']` to my PHP file" - this doesn't make sense. As I stated above, this is generated automatically by PHP, there is nothing to "pass"? And there is no need to parse this to get the "url after `?`" - this is called the _query string_ and is already present in the PHP superglobal `$_SERVER['QUERY_STRING']`. – DocRoot Dec 23 '18 at 17:21
  • I think I get what you are trying to. I've updated my answer. (Asking about _passing_ `REQUEST_URI` would seem to be a [red herring](https://en.wikipedia.org/wiki/Red_herring).) – DocRoot Dec 23 '18 at 17:36
  • OMG!! Yes, this one works great!! thank you soo much, but why it is red herring? because i'm working with it and works but just don't know how to acheive the same result you did with $_SERVER['QUERY_STRING'] using htaccess, do you know? – Robert Dec 23 '18 at 18:52
  • 1
    Glad it works, you're welcome. Please mark it as "accepted" (checkmark below the voting arrows) to remove it from the unanswered question queue. You can also upvote answers you find useful. – DocRoot Dec 23 '18 at 21:39
  • It was a "red herring", because you were asking about a perceived solution, instead of the actual problem. " just don't know how to acheive the same result you did with $_SERVER['QUERY_STRING'] using htaccess, do you know?" - I'm not sure what you mean; that's what this code already does. (?) – DocRoot Dec 23 '18 at 21:42
  • Ok, marked as asnwered :) i'm just asking if possible to acehive it by request $_SERVER['REQUEST_URI'] in my php file instead of $_SERVER['QUERY_STRING'] Do you have another solution for $_SERVER['REQUEST_URI'] to pass it from main index to my php file? – Robert Dec 23 '18 at 21:57
  • The `.htaccess` solution would be the same if using `$_SERVER['REQUEST_URI']`. Except you would need additional PHP code to parse the query string from the `$_SERVER['REQUEST_URI']` PHP variable. – DocRoot Dec 23 '18 at 22:02
  • That URL does not resolve for me? No changes are required for `.htaccess`. Using that code, `$_SERVER['REQUEST_URI']` would return a string of the form `/?http://google.com` for such a request - you would then need to extract everything after the first `?` (to get the same value as is already available in the `$_SERVER['QUERY_STRING']` variable). What is `$_SERVER['REQUEST_URI']` returning for you? – DocRoot Dec 23 '18 at 22:23
  • That suggests you have an error in your PHP code that parses the URL. (As stated above, that is the expected value of `$_SERVER['REQUEST_URI']` - it doesn't look like you code is doing anything?) – DocRoot Dec 23 '18 at 22:31
  • "I think the code and extract url works fine" - How can it be working fine? The result wouldn't be `/?http://google.com` if it was working fine?! It doesn't have to be a "syntax error", it can be a "runtime error" - when you don't get the expected output, given a certain input. Add your PHP code to your question. – DocRoot Dec 23 '18 at 22:39
  • What is the value of `$_SERVER['REQUEST_URI']` that your script is seeing? That will tell you whether `.htaccess` is working OK or not. – DocRoot Dec 23 '18 at 23:01
  • 1
    But that is correct? You'd then extract the query string with something like `parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY);` or `substr(strstr($_SERVER['REQUEST_URI'],'?'),1);` - both result in the same thing, eg. `http://google.com`. – DocRoot Dec 23 '18 at 23:11
  • 1
    yes with your code works fine now, thank you so much for your helps :) – Robert Dec 23 '18 at 23:19
0

Use an environment variable.

.htaccess
SetEnv RURI %{REQUEST_URI}
index.php
<?php $requestURI = getenv('RURI'); ?>

Set an environment variable in .htaccess and retrieve it in PHP

Make sure mod_env is enabled in Apache.

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • tested but does not work, you have to know that i use ?something in main index.php and not folder/index.php i think the way i did it is 50% correct but don't know what missing to make it fully work. – Robert Dec 20 '18 at 22:06
  • Try to put the `SetEnv` before the `RewriteCond`. – Chloe Dec 20 '18 at 22:09
  • sorry, but does not work. any way to get it in my php file folder/index.php using $_SERVER['REQUEST_URI'] ? – Robert Dec 20 '18 at 22:28