0

I'm using IIS without any URL re-writing that include an index.php at the end. The custom permalink structure is: /index.php/%year%/%monthnum%/%postname%/

The problem is that the home_url function doesn't include the index.php, so if I want to refer to a custom page <?php echo home_url() . '/my-page/'; ?> returns http://domain.com/my-page/ but what I need is http://domain.com/index.php/my-page/ .

Is there a WordPress function that will include the index.php or do I have to put in a custom bit of code?

N.B. I've tried Googling, but most links point to re-writing the URL which is not what I want.

1

You can filter home_url:

add_filter( 'home_url', 'wpse102523_home_url' );
function wpse102523_home_url( $url ) {
    return $url . 'index.php/';
}

Reference: Adam Brown's Filter Database

  • Thanks for this - I was hoping for an internal WP function along the lines of base_permalink_url or some sort, but for a custom bit of code this would definitely be the neatest solution. I think that home_url normally leaves off the end slash, so the slash should be the other side of index.php i.e. return $url . '/index.php'; – icc97 Jun 10 '13 at 14:18
  • Ah following on from your Adam Brown database I spotted this function iis7_supports_permalinks. – icc97 Jun 10 '13 at 14:23
  • I hadn't run across that. (But then I'm not running WP on IIS.) You learn something new every day. – Pat J Jun 10 '13 at 14:33
0

Trying the add_filter option didn't actually work for me. The code gets quite ugly as the home_url function was impacting permalinks which have index.php and all my attempts to get it working failed.

The best solution I came up with was to modify the Site Address (aka home_url) in General Settings to be http://domain.com/index.php. Then change the permalinks to be normal e.g. /%year%/%monthnum%/%postname%/. This then gets most links working. The only problem link is http://domain.com/index.php/ (note the end '/'). For me this doesn't matter as its my localhost site, but otherwise just fix that case and the site should work fine.

Just incase any one does want to carry on going down the add_filter route, here for my sins is my attempt:

<?php
if ($is_iis7 && !iis7_supports_permalinks()) {
    if (!is_admin()) {
        add_filter( 'home_url', 'icc_iispb_url' );
    }
}

function icc_iispb_url($url) {
    if (strpos($url, 'index.php') === false) {
        $url = str_replace(get_option('home'), get_option('home').'/index.php', $url);
    }
    return $url;
}   
?>
  • 1
    You might want to clarify where $is_iis7 gets set. (It appears to be a global variable, so you can add global $iis7;, but I'm not 100% sure that's a best practice.) – Pat J Jun 10 '13 at 16:10
  • Yes - the code does get ugly, which is why I switched to modifying the site address which works much better. I only use it as they do use $is_iis7 in the source of the iis_supports_permalinks function. – icc97 Jun 11 '13 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.