Make WordPress Core

Opened 18 years ago

Closed 17 years ago

#3514 closed defect (bug) (fixed)

IIS fix for get_pagenum_link

Reported by: snakefoot's profile snakefoot Owned by: markjaquith's profile markjaquith
Milestone: 2.3 Priority: high
Severity: major Version: 2.0
Component: General Keywords: get_pagenum_link, iis, posts_nav_link, has-patch
Focuses: Cc:

Description

The posts_nav_link doesn't work in categories, when trying to browse to the second page of a category using "Previous entries", then the link points to:

http://example.com/index.php/page/2/

Instead of pointing to:

http://example.com/index.php/category/page/2/

The fix for me was to modify get_pagenum_link located in template-functions-links.php (Both trunk and branch should get this rather easy fix)

function get_pagenum_link($pagenum = 1) {
	global $wp_rewrite;

	// IIS on Windows fix
	if (!$_SERVER['REQUEST_URI']) $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].$_SERVER['PATH_INFO'];

	$qstr = wp_specialchars($_SERVER['REQUEST_URI']);
...

Attachments (1)

wp-setting.diff (1.4 KB) - added by snakefoot 17 years ago.
REQUEST_URI patch for IIS

Download all attachments as: .zip

Change History (15)

#1 @snakefoot
18 years ago

Just discovered that my IIS didn't react to the IIS test ($_SERVER['REQUEST_URI'] returned "/index.php"). Now changed it according to this advice:

http://www.xoops.org/modules/newbb/viewtopic.php?topic_id=29255

function get_pagenum_link($pagenum = 1) {
	global $wp_rewrite;

	if ( !isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI']==$_SERVER['PHP_SELF'])
		$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].$_SERVER['PATH_INFO'];

	$qstr = wp_specialchars($_SERVER['REQUEST_URI']);
...

#2 @markjaquith
18 years ago

  • Milestone changed from 2.0.6 to 2.0.7

Is this an issue for trunk? If so, it should have a 2.2 milestone.

#3 @snakefoot
18 years ago

Unless special updating of REQUEST_URI (if IIS) have been added to 2.1 or 2.2, then it is most likely an issue for the trunk as well (As the get_pagenum_link() is the same in the trunk).

Maybe one should consider creating a global function, which returns the expected URL independent of what PHP/IIS/Apache/Litestep version one is using, and then reuse the function all the locations where REQUEST_URI is used.

Wordpress has different places which tries to handle the REQUEST_URI behavior on IIS, but each implementation is different.

#4 @snakefoot
18 years ago

Discovered that wp_settings.php is the only one updating $_SERVER['REQUEST_URI'], so it is probably not a good idea to manipulate the $_SERVER['REQUEST_URI'] in get_pagenum_link, but instead just append PATH_INFO.

#5 @markjaquith
18 years ago

  • Milestone changed from 2.0.7 to 2.2
  • Owner changed from anonymous to markjaquith
  • Status changed from new to assigned

#6 @rob1n
18 years ago

PATH_INFO is already appended, I think.

#7 @foolswisdom
17 years ago

  • Milestone changed from 2.2 to 2.3

#8 @snakefoot
17 years ago

With the #3930 solved, the get_pagenum_link() looks much prettier and by changing the following lines it should work with IIS (Notice the str_replace):

     if ( !$wp_rewrite->using_permalinks() ) { 
        $base = trailingslashit( get_bloginfo( 'home' ) ); 

        if ( $pagenum > 1 ) { 
            $result = add_query_arg( 'paged', $pagenum, $base . $request );  
        } else { 
            $result = $base . $request; 
        }
    } else {
        $request = str_replace('index.php/', '', $request);
        $request = str_replace('index.php', '', $request);
        
        $request = preg_replace( '|/?page/(.+)$|', '', $request);

Along with modifying REQUEST_URI in wp_settings.php (Will also make WP-Cache work out of the box):

     // Fix for IIS, which doesn't set REQUEST_URI
     if ( empty( $_SERVER['REQUEST_URI'] ) ) {
        $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];

Must admit that I have only tested this on 2.0 (Exchanged the original get_pagenum_link with the new one in version 2.3). Have tested with permalink using index.php without ISAPI rewrite, and without index.php using ISAPI rewrite, and everything (pages, posts, categories, archives, search, paged) works as they should.

#9 @snakefoot
17 years ago

I guess the best solution would be to fix the REQUEST_URI completely. If one could detect that the permalink options is not set to use index.php, then don't include the SCRIPT_NAME in the REQUEST_URI, but just PATH_INFO.

#10 @snakefoot
17 years ago

Just made a plugin with the following code and now no modification is needed in the get_pagenum_link() (Still need the modification in wp_settings.php to make WP-Cache work)

function fix_request_uri()
{
	global $wp_rewrite;

	if ($wp_rewrite->using_index_permalinks())
	{
		$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
	}
	else
	{
		$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
	}

	// Append the query string if it exists and isn't null
	if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']))
	{
		$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
	}
}

add_action('init', 'fix_request_uri');

#11 @snakefoot
17 years ago

Corrected some bugs in the plugin:

function fix_request_uri()
{
	global $is_IIS;
	global $wp_rewrite;
	if (!$is_IIS)
		return;

	if ($wp_rewrite->using_index_permalinks())
	{
		$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
	}
	else if ($wp_rewrite->using_permalinks())
	{
		// SCRIPT_NAME includes script-path + script-filename (remove the filename) and append request-path PATH_INFO
		$_SERVER['REQUEST_URI'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')) . $_SERVER['PATH_INFO'];
	}
	else
	{
		$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
	}

	// Append the query string if it exists and isn't null
	if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']))
	{
		$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
	}
}

@snakefoot
17 years ago

REQUEST_URI patch for IIS

#12 @snakefoot
17 years ago

  • Keywords has_patch added

Added patch for wp-setting.php which fixes this ticket.

#13 @Nazgul
17 years ago

  • Keywords has-patch added; has_patch removed

#14 @markjaquith
17 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [5889]) Set REQUEST_URI for IIS in more situations. props snakefoot. fixes #3514

Note: See TracTickets for help on using tickets.