简体   繁体   中英

Wordpress url rewrite issue for page has custom template

I have an issue of url rewrite. A page created from wp-admin which title suppose profile and a custom template is assigned to profile page. I used custom permalink structure(/%postname%) for the site.

Now I want URL look like this:

http://www.example.com/profile/abc2015

Currently I am passing querystring like

http://www.example.com/profile/?pagename=profile&name=abc2015

I tried to use the following code in theme's functions .php:

 <?php  
      function custom_rewrite_rule() 
      {   
         add_rewrite_tag('%name%', '([^&]+)');           
         add_rewrite_rule('^profile/([^/]*)/?$','index.php?pagename=profile&name=$matches[1]','top');    
      }
      add_action('init', 'custom_rewrite_rule'); 
    ?>

I have not find any solution. Someone can help?

Thank you

Try this

function create_new_url_querystring() {
    add_rewrite_rule(
        '^profile/([^/]*)$',
        'index.php?pagename=profile&name=$matches[1]',
        'top'
    );

    add_rewrite_tag('%name%','([^/]*)'); } add_action('init', 'create_new_url_querystring');

This is not tested but hope this helps. Another useful information which might help your stuff to work.

Flush Rewrite Rules

When you change your WordPress URL structure or add new rewrite rules then the WordPress database will need to be updated with the new URL rules this is so it will understand how to search for your posts from the given URL. Sometimes you get the problem of changing a URL structure and WordPress returning a 404 page for your posts. This can be because the rewrite rules have not refreshed correctly.

There are a few ways you can refresh the permalink rules. First you can navigate to the permalinks page Settings -> Permalinks and change the permalink click the save button, then change it back to the way it was. This will refresh all the rewrite rules on your website and your custom post types should be displayed. Secondly you can open phpMyAdmin, navigate to the wp_options table and delete the rewrite rules record from this table. Next time WordPress loads it will check for the rewrite rules in this table if they aren't there then it will regenerate the rules.

The third option is to place the function flush_rewrite_rules() under the register rewrite rules. This will completely refresh the rewrite rules and fix any problems with redirecting.

flush_rewrite_rules();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM