Where do I USE add_rewrite_rule?

I need to add a RewriteRule to my site. I have tried in .htaccess with no luck, it doesn’t get parsed, so I’m all but giving up on that. I found the documentation on add_rewrite_rule, but have no idea where I’m supposed to use it. I am not writing a plugin. I need to put in one simple rule.

I went to the functions.php file in what is the current theme folder. Is this the one I use? I inserted the following:

Read More
add_action( 'init', 'add_news_rule3' );
function add_news_rule3() 
{ add_rewrite_rule( '^/content/([^/]*)/([^/]*).htm$', '/content/$matches[1] [NC,R=301,L]', 'top' ); } 

I’ve also tried, based on a post I saw elsewhere:

add_action( 'generate_rewrite_rules', 'add_news_rule' );
function add_news_rule($wp_rewrite)
{ $new_rules = array('^/content/([^/]*)/([^/]*).htm$' => '/content/$matches[1] [NC,R=301,L]'); 
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } 

add_filter('rewrite_rules_array','add_news_rule2'); 
function add_news_rule2($rules)
{ $new_rules = array('^/content/([^/]*)/([^/]*).htm$' => '/content/$matches[1] [NC,R=301,L]'); 
return $new_rules + $rules; } 

Related posts

Leave a Reply

3 comments

  1. add_rewrite_rule is for converting a URL structure to query vars and routing requests through index.php, you can’t point to other URLs and use flags like you would in htaccess rewrites.

    for what you’re trying to achieve, doing it via htaccess is the simplest way. your rule should work if you just remove the leading slash:

    RewriteRule ^content/([^/]+)/([^/]+).htm$ /content/$1/ [NC,R=301,L]
    

    This would need to go before any WordPress rewrites if you’re using pretty permalinks, otherwise this rule will never be reached.

  2. If possible Can you please show your .htaccess code so we can see that what exactly problem you are facing.

    But first of all check whether your mod_rewite module is enabled or not in your web server.
    To find out it is enabled or not you just put a file in your webserver name phpinfo.php

    put this Code phpinfo.php file

    <?php phpinfo(); ?>
    

    once this file execute you will see under heading apache2handler in table see Loaded Modules and find mod_rewrite module is present or not. If not present then contact to your hosting provide and make it enable.

    If it is enabled then check your .htaccess file once again may be you are missing something.

  3. If you do something like a Database query or anything that you don’t want to run a lot, you better use this approach:

    add_action(
        "update_option",
        function ($option, $old, $new) {
            if ($option === "rewrite_rules" && $new === "") {
                add_rewrite_rule(
                    "^verify55/(?P<hi>[^/]++)/(?P<hello>[^/]++)(?:/(?P<why>[^/]++))?$",
                    "index.php?page_id=" . url_to_postid("/verify"),
                    "top"
                );
            }
        },
        10,
        3
    );
    

    This will only run while flushing the rewrite rule, therefore here my url_topostid which does a database query doesn’t run on every request. 🙂

    That makes much more sense than to put it in init hook.

    Remember it’s always the little things that ends up slowing down your website, gradually!