Dmitry Kazakov
Dmitry Kazakov

Reputation: 1679

How to use REQUEST_URI instead of REDIRECT_URL with IIS to get same functionality?

I have PHP app with usage of $_SERVER['REQUEST_URI']. If I use Apache web server my app is working fine. If I use Microsoft IIS web server App is not working, because $_SERVER['REQUEST_URI'] is null. I found that $_SERVER['REDIRECT_URL'] is available with Microsoft IIS.

My question is how to use $_SERVER['REDIRECT_URL'] instead of $_SERVER['REQUEST_URI'] to get same functionality using Microsoft IIS web server?

I can not simply replace REQUEST_URI with REDIRECT_URL, as it does not contain the query string and I need that for my PHP app.

My php code:

function filterAppUrl($url)
{  
  $url = htmlspecialchars($url);
  $url = str_replace('"', '',  $url);
  $url = str_replace("'", '',  $url);
  return $url;
}

function setAppURL()
{
   $url = $this->filterAppUrl($_SERVER['REDIRECT_URL']);
   $url = substr($url, 1, strlen($url));
   $this->full_url = $url;
   $this->url = explode('/', $url);
}

My .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

My Web.config for IIS

<?xml version="1.0"?>
<configuration>
 <system.webServer>
 <defaultDocument>
  <files>
    <remove value="index.php" />
    <add value="index.php" />
  </files>
 </defaultDocument>
<rewrite>
 <rules>
     <rule name="Main Rule" stopProcessing="true">
         <match url=".*" />
         <conditions logicalGrouping="MatchAll">
             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
         </conditions>
         <action type="Rewrite" url="index.php" />
     </rule>
 </rules>
</rewrite>
</system.webServer>
</configuration>

Upvotes: 2

Views: 3911

Answers (2)

DavidKunz
DavidKunz

Reputation: 325

I had the same problem, and although the exact contents of the $_SERVER array seem to differ between various IIS configurations, this works for me on an Azure machine.

I use this code to get a working reference to the requested URL without a query string, on both Unix and IIS:

        // on unix we just get what we need
        if(isset($_SERVER['REDIRECT_URI'])) {
            $base_uri = $_SERVER['REDIRECT_URI'];
        }
        // IIS doesn't have a nice way to get the request url without the query string,
        // so grab the request uri until the start of the query string
        else {
            $base_uri = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'));
        }

For an url like http://www.example.com/path/to/page?article=123, $base_uri will be, on both Unix and IIS, /path/to/page.

Upvotes: 0

Martin Schapendonk
Martin Schapendonk

Reputation: 13536

Something like:

function setAppURL()
{
   if (isset($_SERVER['REDIRECT_URL'])) {
      $url = $this->filterAppUrl($_SERVER['REDIRECT_URL']);
   } else {
      $url = $this->filterAppUrl($_SERVER['REQUEST_URI']);
   }
   $url = substr($url, 1, strlen($url));
   $this->full_url = $url;
   $this->url = explode('/', $url);
}

Which basically is like saying: if REDIRECT_URL is set and not null, then use it. Else use REQUEST_URI.

Upvotes: 1

Related Questions