tags:

views:

14147

answers:

9

I moved a WordPress installation to a new folder on a Windows/IIS server. I'm setting up 301 redirects in PHP, but it doesn't seem to be working. My post URL's have the following format:

http:://www.example.com/OLD_FOLDER/index.php/post-title/

I can't figure out how to grab the /post-title/ part of the URL.

$_SERVER["REQUEST_URI"] - which everyone seems to recommend - is returning an empty string. $_SERVER["PHP_SELF"] is just returning index.php.

+2  A: 

$_SERVER['REQUEST_URI'] doesn't work on IIS, but I did find this: http://neosmart.net/blog/2006/100-apache-compliant-request_uri-for-iis-and-windows/ which sounds promising.

Greg
+4  A: 

Maybe

$_SERVER['PATH_INFO']

is what you want, based on the URLs you used to explain

Vinko Vrsalovic
That did the trick. Thanks.
Nick Hebb
+1  A: 

REQUEST_URI is set by Apache, so you won't get it with IIS. Try doing a var_dump or print_r on $_SERVER and see what values exist there that you can use.

Peter Bailey
+1  A: 

REQUEST_URI is an Apache-specific value, so won't be in IIS by default. There's a workaround described here, which involves following the installation instructions linked to by RoBorg above.

For future reference, there's a very handy table here which lists the server-type variables made available to you in both Apache and IIS.

ConroyP
A: 

The posttitle part of the url is after your index.php, which is a common way of providing friendly urls without using mod rewrite. The posttitle is actually therefore part of the query string, so you should be able to get it using $_SERVER['QUERY_STRING']

(underscores removed due to formatting)

adam
+1  A: 

I use the following function to get the current, full URL. This should work on IIS and Apache.

function get_current_url() {
    $protocol = 'http';
    if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) {
        $protocol .= 's';
        $protocol_port = $_SERVER['SERVER_PORT'];
    } else {
        $protocol_port = 80;
    }
    $host = $_SERVER['HTTP_HOST'];
    $port = $_SERVER['SERVER_PORT'];
    $request = $_SERVER['PHP_SELF'];
    $query = substr($_SERVER['argv'][0], strpos($_SERVER['argv'][0], ';') + 1);
    $toret = $protocol . '://' . $host . ($port == $protocol_port ? '' : ':' . $port) . $request . (empty($query) ? '' : '?' . $query);
    return $toret;
}
Jrgns
argv won't work if you're not using a CGI-based Apache or IIS, I think. I tried your code above on Apache2 in regular mode (not CGI mode) and was erroring out because $_SERVER['arv'][0] is not an index. Note also that I have full PHP error reporting turned on and these errors were notice errors.
Volomike
+3  A: 
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
Chacha102
A: 

I have use the following code and I am getting right result....

<?php
    function currentPageURL() {
    $curpageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$curpageURL.= "s";}
    $curpageURL.= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    $curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
    $curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $curpageURL;
    }

    echo currentPageURL();
?>
Joomla Developers
A: 
function my_url(){

    $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    echo $url;

}

then just call my_url function.

Avery