0

I have created a plugin and i have gotten rewrite rules to work using my .htaccess file. I would like to make this work from my plugin instead to keep everything contained from within there.

Im trying to do this via the wordpress rewrite but nothing i do it working with this. I have even tried a basic rewrite and it wont work either. readme.html is a page in the root directory. I simply want testpage to display this page. The other rules i have in my .htaccess are far more complicated than this so, if i cant get this one to work i have no hope with the complicated ones.

    add_action('init', 'add_actor_url');
    function add_actor_url()
    {
        add_rewrite_rule('^testpage','readme.html','top');
        flush_rewrite_rules();
    }
2
  • Your code works perfectly fine for me. (Although I would advise against flushing rules on every call.) Maybe try vardumping something in that function to see if it get's called. Commented Jul 29, 2015 at 10:25
  • If i go to the main menu and go to permalink settings and just save them without making a change i can see these rules are getting written to the .htaccess file. Is there a way to know if these rules exist and only apply/flush them if this happens? Commented Jul 29, 2015 at 10:55

1 Answer 1

1

You need to flush rewrite rules after adding or modifying rewrite rules in WordPress.

flush_rewrite_rules() can be used to flush rules.

For more info check these links :

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

1
  • This didnt work, but you are on the right road. I went to the admin panel and in the permalinks menu i just clicked save. This made all of the rules write to the .htaccess file Commented Jul 30, 2015 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.