How to use add_rewrite_rule function, while permalink structure is disabled?

I am using the add_rewrite_rule() function to modify my URL structure.

I’m wanting to use add_rewrite_rule to add a custom rule but these rules only get added in when other than default settings are selected in my permalink settings area.

Read More

i.e. in the settings there are following options:

 - Default             http://localhost/wordpress/?p=123

 - Day and name        http://localhost/wordpress/2014/08/14/sample-post/

 - Month and name      http://localhost/wordpress/2014/08/sample-post/

 - Numeric             http://localhost/wordpress/archives/123

 - Post name           http://localhost/wordpress/sample-post/

 - Custom Structure    http://localhost/wordpress

So, when I select other then ‘Default’, my add_rewrite_rule() function works, but while selecting ‘Default’, the function doesn’t seem to be work. So please suggest me how to work the function in any condition. Any help would be Appriciated.

Update:

I think the problem lies here:
When I use this, while selecting ‘Default’:

get_option('permalink_structure');

I got nothing.

While in the other cases, there are some values like:

/%postname%/

/archives/%post_id%

/%year%/%monthnum%/%postname%/

Related posts

Leave a Reply

3 comments

  1. The Default permalinks, or so called “Ugly” permalinks, are not adding anything to the .htaccess file, so the Apache rewrite engine is not enabled. Without the rewrite engine, no rewrites can be done. So the short answer is that rewrites are not possible with Default permalinks.

    I can recommend you to use rewrites along with query vars. When adding a rewrite rule, pass your custom data to a query var, and build the functionality around that query var. This way your functionality will work in all situations and with all permalink types.

    So for example if you have the following rule:

    add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
    

    and you have the custom_query_var added as a query var by using the following code:

    function add_query_vars_filter( $vars ){
        $vars[] = "custom_query_var";
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
    

    then when Default permalinks are selected, the following URL would work for you:

    http://yoursite.com/index.php?custom_query_var=abc

    and if “Pretty” permalinks are selected, the URL rewriting would work and your URL would look the following way:

    http://yoursite.com/sometest/abc/

    which is basically the best that can be achieved with rewrites.

  2. use this:
    function my_add_query_vars( $qvars ) {
    
    
            $qvars[] = 'business-coaching';
            $qvars[] = 'country';
            $qvars[] = 'territory'; 
            $qvars[] = 'region';        
    
        return $qvars;
    
    }
    add_action('query_vars', 'my_add_query_vars');
    //Write the rule 
    function add_analytic_rewrite_rule()
    {
        // Regex:The regex to match the incoming URL is:business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?
        // Redirect Rule :The resulting internal URL: `index.php` because we still use WordPress
        // `pagename` or page_id=45 because we use this WordPress page
        // `country` : we will assign the first captured regex part to this variable
        // `territory` we will assign the second captured regex part to this variable
        // `region` we will assign the third captured regex part to this variable
        add_rewrite_rule('business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?','index.php?page_id=45&country=$matches[2]&territory=$matches[`enter code `enter code here`here`4]&region=$matches[6]','top');//superfinal
    
    }
    add_action('init', 'add_analytic_rewrite_rule');