• Resolved itsjaked

    (@itsjaked)


    Hello,

    I am working on a custom plugin and I have the following rewrite rule:

    add_rewrite_rule(
    '(.+?)/([^/]+)/([^/]+)/?$',
    'index.php?pagename=$matches[1]&state=$matches[2]&city=$matches[3]',
    'top'
    );

    It’s working on all pages but the homepage. Whenever I try to pass parameters to the homepage it automatically redirects to the root.

    For example if I type in domain.com/state/city it send me straight to domain.com and I am not able to get the parameters.

    I have tried to add the following rewrite rules with no avail

    add_rewrite_rule(
    '([^/]+)/([^/]+)/?$',
    'index.php?pagename=home&state=$matches[1]&city=$matches[2]',
    'top');
    
    add_rewrite_rule(
    '(.+?)/([^/]+)/([^/]+)/?$',
     'index.php?state=$matches[2]&city=$matches[3]',
    'top'
    );

    I’m guessing there is a built in WordPress redirect that automatically redirects anything referencing the static homepage to the root, but I’m not sure.

    Any ideas on how to get this working?

    Thanks so much!
    Jake

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You’re right, WP has a series of internal redirects via redirect_canonical(). You can alter this behavior through the ‘redirect_canonical’ filter. Your callback could check the original URL for the home page slug and return the original request instead of the root URL normally returned.

    Hi I have had the same problem with rewrites of the home page – my script was working for more than a year when a WordPress update silently changed its behaviour, creating redirects to the home page and removing the rest of the url.

    It’s a rewrite of this form:
    $new_rules = array(‘keyword/(.*)’ => ‘index.php/?page_id=123&keyword=’.$wp_rewrite->preg_index(1),);

    which worked with urls like example.com/keyword/item where the page pulled content for “item” from a database.

    My only option now is to move this off the front page of the site. Tried changing index.php/? to just /? and just ? and although that returns a page at the desired url, it does not serve the content of the home page.

    Tried every permutation of redirect_canonical() without success. If someone can pose a working example I would be grateful.

    • This reply was modified 8 years, 3 months ago by virtre.
    Moderator bcworkz

    (@bcworkz)

    Here’s a generic example. It goes in functions.php of your active theme. If it solves your problem, seriously consider creating a child theme to contain your custom code or it’ll be lost when your theme updates.

    It doesn’t do anything as is, but if you alter the first part of the if () conditional, you can have it return any sort of URL you require. By just changing false to true you totally disable all canonical redirects. You can uncomment the error_log() line to have the original request of every redirect written to your error log. The information can be useful in composing a proper conditional statement.

    add_filter( 'redirect_canonical', 'vir_override_canonical', 10, 2 );
    function vir_override_canonical( $redirect_url, $requested_url ) {
       //error_log("Requested non-canonical URL: $requested_url");
       if ( false ) {
          return $requested_url;
       } else {
          return $redirect_url;
       }
    }

    I hope this helps.

    Hi bcworkz Thanks for the code, that works and solved my problem. My code was already in functions.php so I just added your code with one change to make it apply to just the home page (currently the page with id of 123).

    
    add_filter( 'redirect_canonical', 'vir_override_canonical', 10, 2 );
    function vir_override_canonical( $redirect_url, $requested_url ) {
       //error_log("Requested non-canonical URL: $requested_url");
       $id = get_the_ID();
       if ($id == 123)
       return $requested_url;
       } else {
          return $redirect_url;
       }
    }

    I can confirm that works in both PHP 5.6 and PHP 7.

    • This reply was modified 8 years, 3 months ago by virtre.
    Moderator bcworkz

    (@bcworkz)

    Very nice. Thanks for reporting back. I’ll go ahead and mark this topic as resolved.

    I’m quite sure it’ll work with all earlier PHP versions too, though not actually confirmed ??

    Bunty

    (@bhargavbhandari90)

    For me this is not working. I don’t know what I did wrong. I added this rule

    add_rewrite_rule( 'pg/([0-9]*)/?', 'index.php?page_id=' . get_option( 'page_on_front' ) . '&pg=$matches[1]', 'top' );

    and trying to visit mysite.com/pg/2/ and redirecting to mysite.com/.

    Note: I set one of my page as “Front Page”.

    • This reply was modified 8 years, 2 months ago by Bunty.
    Bunty

    (@bhargavbhandari90)

    Ok found my solution. Above solution works for me after adding some conditions according to my requirements.

    Thanks @bcworkz

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Rewrite Rule with Front Page’ is closed to new replies.