I'm trying to develop a plugin which adds a single line to the .htaccess file generated by WordPress.

The problem is that one of the WP generated lines override my rule, and via the "insert_with_markers()" function, I have not found a way to specify a position so I could prepend my line instead of appending it as currently is happening.

The first line of code is the auto generated WordPress line, and the second one is the one I'd like to have working.

RewriteRule . /dev/index.php [L]

RewriteRule ^article/([/_0-9a-zA-Z-]+)$ http://www.domain.tld/?id=$1 [R=301,L,NC]

The way I see it there's 2 possibilities here:

A) Somehow specify where the insert_with_markers() places the new line of code, or

B) Modify my RewriteRule to work together with the WordPress generated one.

I hope you can point me a step or two closer to the right path.



I would suggest an alternative route: the rewrite API - did you know it supports external URLs*?

function wpse_199898_add_htaccess_rule() {
    // No need for the caret "starts with", WP will add it
    add_rewrite_rule( 'article/([/_0-9a-zA-Z-]+)$', 'http://www.domain.tld/?id=$1 [R=301,L,NC]' );

add_action( 'init', 'wpse_199898_add_htaccess_rule' );

*Almost. We just need a fix for WP's quirky path prefixing:

function wpse_199898_fix_htaccess_rule( $rules ) {
    return str_replace( '/http://www.domain.tld/', 'http://www.domain.tld/', $rules );

add_filter( 'mod_rewrite_rules', 'wpse_199898_fix_htaccess_rule' );
  • Thanks, I was gonna do some more digging around the filters and so on, but seems like the rewrite API seems to be exactly what I need, and these examples look great so I'll accept your answer. – Mark Aug 27 '15 at 13:48

After reading up on the issue I decided to go a different route altogether, and hook into filters instead of using insert_with_markers()

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.