• Resolved andrewbecks

    (@andrewbecks)


    I’ve read through the documentation about setting up rewrites in WordPres using custom_rewrite_basic, but am having some trouble.

    In a nutshell, I’m working on a site with a video category page:

    example.com/article/category/videos/

    The category page takes a querystring param ?video=XXXX where XXXX is an ID that is used to set the default video.

    I’d like to mod_rewrite from:

    /article/category/videos/XXXX to /article/category/videos/?video=XXXX

    I’ve determined that the category is category 9, and tried the following rule with no luck:

    function custom_rewrite_basic() {
      add_rewrite_rule('^article/category/videos/([^/]*)$', '/index.php?cat=9&video=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    
    // Get ad zone ID detail
    include(get_template_directory().'/301adconfig.php');

    Any assistance would be greatly appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Check this, perhaps it will help you..
    How can I create a page with a specific URL?

    Thread Starter andrewbecks

    (@andrewbecks)

    Thanks for the suggestion, satishpandit. I’m currently using permalinks for posts, but what I’m trying to do applies to the category page, so I’m afraid that the link you provided didn’t give me quite what I needed.

    Could you try…

    function custom_rewrite_basic() {
      add_rewrite_rule('^article/category/videos/([^/]*)$', 'index.php?cat=9&video=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');

    Also, could you tell ID XXXX will be numbers or mixed?

    Thread Starter andrewbecks

    (@andrewbecks)

    Thanks, Dipak. That didn’t seem to work, either. (Still returns a 404 error.) I’m putting this in my functions/user/functions.php file (within the theme directory).

    The XXXX ID will only be numbers.

    Thread Starter andrewbecks

    (@andrewbecks)

    If I navigate directly to index.php/?cat=9&video=517339714, I’m successfully redirected to
    article/category/videos/?video=517339714, so I know that the category ID is correct.

    @andrewbecks, Why THEME_NAME/functions/user/functions.php, not THEME_NAME/functions.php directly?

    Try this…

    function custom_rewrite_basic() {
      add_rewrite_rule('^article/category/videos/?video=([0-9]+)/?', 'index.php?cat=9&video=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');

    IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after adding above function into functions.php. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

    Thread Starter andrewbecks

    (@andrewbecks)

    You are a genius! Thanks, Dipak. I was missing the step where you save the Permalinks to regenerate and flush the rewrite rules from the database.

    Thanks again!

    @andrewbecks, No problem. I am glad that it fixed.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom rewrite rule now working with custom_rewrite_basic’ is closed to new replies.