Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add some rewrite rules to a plugin that is supposed to redirect to different urls, basically rotates through urls I add in the admin.

Example: The plugin creates mydomain.com/recommends/walmart/ and when a visitor hits this url he gets redirected. The /recommends/ part does not exist as page or post on my wordpress install and is only supposed to make the urls look nice and the user can enter in the plugins settings page the name that should be used for this redirect directory...

Here is the code I am using:

    // Add rewrite rule and flush on plugin activation
register_activation_hook( __FILE__, 'aff_redirs_activate' );

function aff_redirs_activate() {

    aff_redirs_rewrite();
    flush_rewrite_rules();

}
// Flush on plugin deactivation
register_deactivation_hook( __FILE__, 'aff_redirs_deactivate' );

    function aff_redirs_deactivate() {

flush_rewrite_rules();

}


// Create new rewrite rule
add_action( 'init', 'aff_redirs_rewrite' );

function aff_redirs_rewrite() {

add_rewrite_rule( "{$aff_path}/([^/]+)/?$", "redirect.php?affredir=true&redirect=$matches[1]", "top" );

}

The weird thing is now, when I use index.php in my rewrite rule(s) it doesn't add it to the htaccess on my local install, when I use anything else it works without problems

add_rewrite_rule( "{$aff_path}/([^/]+)/?$", "index.php?affredir=true&redirect=$matches[1]", "top" );

Can anybody help me out here, I'm really going nuts over this "little" issue :) Thank you!

share|improve this question
So you just want 'recommends' to be added to your URL? I don't see the term in your rewrite rule though. – Rutwick Gangurde Feb 24 '12 at 3:52
2  
add_rewrite_rule() doesn't add to .htaccess. it gets added to an option in the wp_options table – helgatheviking Feb 24 '12 at 5:05

closed as too localized by toscho Dec 15 '12 at 23:41

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Thanks figured it out, when I add a slash at the beginning it works and is added to the htaccess.

PS: of course it's added to the htaccess? that is what it's for, you can set it to do a soft rewrite then it's only added to the cache, default is hard rewrite = add to htaccess.

share|improve this answer
Just to be clear add_rewrite_rule does nothing to htaccess. WordPress uses rewrite rule regex (including custom rules) to match request urls that get rewriten to index.php. It's regex based routing in the application (WordPress) itself, not at the server level in htaccess. – chrisguitarguy Oct 21 '12 at 4:19
Not correct :) codex.wordpress.org/Rewrite_API/add_rewrite_rule rewrite rules are actually added to the .htaccess file, not just used internally. Whenever I use add_rewrite_rule, the actual rule is written to my .htaccess file, always been like that. – Marc Oct 21 '12 at 6:58
Which is, of course, why rewrites work on server software other than apache: they write to a non-existent (or not respected) server configuration file. It really doesn't write to htaccess, I promise. – chrisguitarguy Oct 21 '12 at 7:20
Why did my rules then show up in the htaccess file lol :) Used top parameter... – Marc Oct 21 '12 at 7:24

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