WordPress.org

Ready to get started?Download WordPress

Forums

Rewriting custom url problem (2 posts)

  1. UnPointZero
    Member
    Posted 2 years ago #

    Hi there !

    I've some troubles with custom url rewriting ... Let me explain :)

    I've this code on my functions.php file :

    // Ajout rewrite rules
    function flush_rewrites() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    }
    
    function add_rewrites($rules) {
    global $wp_rewrite;
    $ftc_new_non_wp_rules = array(
    	'templates-wordpress/page/(\d+)' => 'index.php?pagename=templates-wordpress&pnum=$1',
    	'templates-wordpress/categorie/(\d+)/page/(\d+)' => 'index.php?pagename=templates-wordpress&catid=$1&pnum=$2',
    	'templates-wordpress/categorie/(\d+)' => 'index.php?pagename=templates-wordpress&catid=$1'
    );
    
    return $ftc_new_non_wp_rules + $rules;
    }
    
    add_filter('rewrite_rules_array', 'add_rewrites');
    add_action('admin_init', 'flush_rewrites');

    The problem is that WordPress doesn't add this rules to the htaccess file BUT I can see with the Rewrite rules inspector plugin (available here : http://wordpress.org/extend/plugins/rewrite-rules-inspector/ ) that's the rules seems to work good ... :

    • templates-wordpress/page/(\d+) index.php?pagename=templates-wordpress&pnum=$1 other
    • templates-wordpress/categorie/(\d+)/page/(\d+) index.php?pagename=templates-wordpress&catid=$1&pnum=$2 other
    • templates-wordpress/categorie/(\d+) index.php?pagename=templates-wordpress&catid=$1 other

    But not on my htaccess file :

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Any help ?

    Thanks :)

  2. UnPointZero
    Member
    Posted 2 years ago #

    Ok think I've found a fix, here's the code :

    // Ajout rewrite rules
    function ftc_flush_rewrites() {
     global $wp_rewrite;
     $wp_rewrite->flush_rules();
    }
    
    function ftc_add_rewrites() {
     global $wp_rewrite;
     $ftc_new_non_wp_rules = array(
    	'templates-wordpress/page/(\d+)' => '/index.php?pagename=templates-wordpress&pnum=$1',
    	'templates-wordpress/categorie/(\d+)/page/(\d+)' => '/index.php?pagename=templates-wordpress&catid=$1&pnum=$2',
    	'templates-wordpress/categorie/(\d+)' => '/index.php?pagename=templates-wordpress&catid=$1'
     );
    
     $wp_rewrite->non_wp_rules = $ftc_new_non_wp_rules;
    }
    
    function add_rewrites_php($ruleswp) {
    global $wp_rewrite;
    $ftc_new_non_wp_rules = array(
    	'templates-wordpress/page/(\d+)' => 'index.php?pagename=templates-wordpress&pnum=$1',
    	'templates-wordpress/categorie/(\d+)/page/(\d+)' => 'index.php?pagename=templates-wordpress&catid=$1&pnum=$2',
    	'templates-wordpress/categorie/(\d+)' => 'index.php?pagename=templates-wordpress&catid=$1'
    );
    
    return $ftc_new_non_wp_rules + $ruleswp;
    }
    
    add_filter('rewrite_rules_array', 'add_rewrites_php');
    
    add_action('generate_rewrite_rules', 'ftc_add_rewrites');
    add_action('admin_init', 'ftc_flush_rewrites');

    Anyone can tell me if there's a way to optimize that ?

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic