I want to add a custom rewrite rule to my .htaccess file using a function inside functions.php

The rewrite rule I want to add is as follows:

RedirectMatch 301 ^/author/(.+)$ http://name-blog.com/$1

I know there is a WordPress function that adds a rewrite rule to the .htaccess:

$wp_rewrite->add_external_rule( 'mobile/([^/]+)$', 'mobile/index.php?action=$1' );

So, how can I use that to add my rewrite rule to .htaccess?

  • 2
    Please add your solution as a answer and come back in two days and accept your answer Apr 19 '14 at 5:46

I found the solution on the Codex just after posting on the site and this is the code I was looking for:

function my_htaccess_contents( $rules )
$my_content = <<<EOD
\n # BEGIN My Added Content
# Protect wpconfig.php
<Files wp-config.php>
    Order Allow,Deny
    Deny from all
# END My Added Content\n
    return $my_content . $rules;
add_filter('mod_rewrite_rules', 'my_htaccess_contents');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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