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
</Files>
# END My Added Content\n
EOD;
return $my_content . $rules;
}
add_filter('mod_rewrite_rules', 'my_htaccess_contents');
Related Posts:
- SEO Friendly URLs for my plugin categories
- Custom rewrite rules are sending everything to index.php
- WordPress keeps writing rewrite rules to .htaccess
- WordPress Rewrite
- Dynamic URL, not a physical page within the database
- Custom rewrite rule
- hard flush_rewrite_rules() not regenerating .htaccess
- Map alt domain to specific section of website w/o MU
- Rewrite rule to load images from production does nothing
- WordPress rewrite rules don’t need ^?
- Url Rewriting a dynamic wordpress page
- My WP_options db rewrite_rules Does Not Work
- Disable Pagination on Pages
- .htaccess rewrite
- Changed pagination URLS to use p= instead of paged=
- Rewriting “pretty” blog category URL with htaccess / add_rewrite_rule() causes 404 page
- WordPress keeps writing rewrite rules to .htaccess
- Customise particular RSS permalink
- RSS feed rewrite matching wrong rule
- Rewrite specific action url
- How do I make WordPress revise an .htaccess file a certain way?
- Rewrite rule that wp-login.php?action=register is left alone
- Pretty URL via Rewrite
- Migration from old CMS to WordPress 301 redirection rules?
- Additional .htaccess rules based on wp page
- What is the proper way to use add_rewrite_rule to remove the (question mark)?
- Can’t get pretty permalinks to work without index.php
- Rewrite Page Parameters
- WP-Include rewrite directory
- How do you create a “virtual” page in WordPress
- Problem with add_rewrite_rule and pagination (paged and page query_vars)
- How do I remove a word from a url in WordPress using .htaccess?
- When to call `add_rewrite_rule` for safety
- Rewrite rules goes away
- Rewrite Rule for Custom Page with Query Vars in URL
- Rewrite Rule for Post “Subpage”, with pagination
- unexpected problem in url rewrite
- Need to make a php file inside theme accessible via url
- WP Rewrite the last two parts of the URL
- Rewrite loading custom template file but is_home() returns true, and there’s no 404
- Rewrite rule for admin-ajax.php
- WordPress redirection to get url friendly
- Access $_POST data after redirect
- Change Query String to pretty permalink
- How to add a custom redirect rule for subdomains?
- Remove special characters in a URL
- Add query string to url and display it as normal url part /folder/
- Is it possible to remove feeds from rewrites?
- Alias ‘wp-content’ directory to something shorter (framework?)
- Flush Rewrite Rules on init or rest_api_init?
- WordPress .htaccess – route other URLs to another app
- Rewrite URL – insert custom variables as a directory path
- How to properly rewrite url by custom var
- Can’t add external rewrites
- url rewrite parsing a custom url parameter not working
- How to add custom variable in url without redirect?
- Using category slug in add_rewrite rule
- add_rewrite_rule ignoring other params than the “p” param
- How to catch Rewrite rules then display a specific post?
- Adding two rewrite rules
- Load an url with minimal/no DB queries
- Auto generate rewrite rules for multiple taxonomies
- Change htaccess to redirect to index.php in subfolder
- Wildcard forward all posts and pages with few exceptions
- Help with Rewrite Rule for Custom Post Type with Query String
- Rewrite URLs – Custom Post Type – Post Slug, Taxonamy Slug
- URL Rewriting for PHP script on an image URL
- Create Dynamic SEO Friendly URL for Virtual Page
- add_rewrite_rule with query variables
- Replace specific middle part of url
- Rewrite query string to path
- Rewrite API -Adding a parameter before the slug
- Rewriting a subdomain page on one WordPress installation to a parent domain on another
- Rewrites rules disappear after a while
- Rewrite vs Redirect from ?p={ID}
- Adding a rewrite rule to page that has no fixed variables and pagination
- Flush rewrite rules on option update with Settings API
- Having WordPress control only certain pages with .htaccess?
- Localize URL bases
- Help with a custom rewrite
- Using URL rewrite to add custom URL for custom post type
- 301 Rewriting htaccess
- Proper way to set up rewrite with Wp
- Rewrite custom taxonomy slug – but only parent items
- Exclude subfolder in WordPress permalink
- wordpress path generation from rewrite rule
- WordPress pagination broken for page 2,3 with custom permalink. Redirects to baseurl
- Help with .htaccess setup to hide WordPress Directory
- Ugly URLs when there’s pagination
- How do I rewrite URL that has custom parameter
- Why is there a 404 on page 2+ for my search page?
- .htaccess RewriteBase equivalent for WordPress – Passing a URL as a variable without getting a 404
- Change Admin URL
- URL Rewrite for CPT single posts
- Using add_rewrite_tag to create urls for page sections
- Rewriting WordPress URLs
- How to rename the WordPress wp-login.php running on IIS6?
- Programmatically add a custom url route “/category/index.json” to return a collection of posts in json
- Rewrite URLs in a Custom Post Type with Multiple Taxonomies
- WordPress picks a wrong template if ?cat= is used as URL parameter