• i don’t manage to make a rewrite work. I’m using a plugin to register it:

    llamadas/([a-z_]+).html$ index.php?page_id=1051&pais=$matches[1]

    If I type in the code manually it works, for example: /index.php?page_id=1051&pais=argentina

    WordPress changes the URL to /llamadas/?pais=argentina and loads the page correctly.

    I want the URL to look like “llamadas/countrynamehere.html” and display correctly. I’ve got the rewrite on top before the others, but no luck.

    Any ideas?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Maybe explicitely adding your custom query “pais” helps:

    function addCustomQueryVars( $query_vars ) {
    
      // add more if you like
      $query_vars[] = 'pais';
    
      return $query_vars;
    
    }
    add_filter( 'query_vars', 'addCustomQueryVars' );

    Then you should be able to access it via get_query_var( 'pais' )

    And just a minor tweak for the regular expression: The dot before the “html” should be escaped. Otherwise it’ll be interpreted as a metacharacter (matching all single characters).

    It should work both ways but “llamadas/([a-z_]+)\.html$” would be clearer in terms of intentions.

    Thread Starter rolover

    (@rolover)

    thanks Chris, if you put in querystring page loads fine. It pulls values ok, problem is not reading rewritten URL, does the filter help that issue?

    How exactly do you register the rule? This approach works for me:

    function addCustomQueryVars( $query_vars ) {
    
      $query_vars[] = 'pais';
    
      return $query_vars;
    
    }
    add_filter( 'query_vars', 'addCustomQueryVars' );
    
    function addCustomRewriteRules() {
    
      add_rewrite_rule( 'llamadas\/([a-z_]+)\.html?', 'index.php?page_id=1051&pais=$matches[1]', 'top' );
    
    }
    add_action( 'init', 'addCustomRewriteRules' );

    You should then go to “settings/permalink” in the WP backend and click the “Save Changes” button each time you add, edit or remove a rule.
    No change needs to be made. But clicking the “Save Changes” button will flush the list of rules so it’s really uptodate.

    For the purpose of testing I’ve edited my theme’s page.php

    global $post;
    
    if( $post->ID === 1051 ) {
    
      $pais = get_query_var( 'pais' );
    
      var_dump( $pais );
    
      exit;
    
    }

    and it’s giving me whaterver llamadas/countryname.html is.

    Thread Starter rolover

    (@rolover)

    Hi Chris, thanks, I added this code to functions and flushed but it doesn’t display the page with rewrite.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to rewrite urls’ is closed to new replies.