I need to add a RewriteRule to my site. I have tried in .htaccess with no luck, it doesn't get parsed, so I'm all but giving up on that. I found the documentation on add_rewrite_rule, but have no idea where I'm supposed to use it. I am not writing a plugin. I need to put in one simple rule.

I went to the functions.php file in what is the current theme folder. Is this the one I use? I inserted the following:

add_action( 'init', 'add_news_rule3' );
function add_news_rule3() 
{ add_rewrite_rule( '^/content/([^/]*)/([^/]*)\.htm$', '/content/$matches[1] [NC,R=301,L]', 'top' ); } 

I've also tried, based on a post I saw elsewhere:

add_action( 'generate_rewrite_rules', 'add_news_rule' );
function add_news_rule($wp_rewrite)
{ $new_rules = array('^/content/([^/]*)/([^/]*)\.htm$' => '/content/$matches[1] [NC,R=301,L]'); 
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } 

function add_news_rule2($rules)
{ $new_rules = array('^/content/([^/]*)/([^/]*)\.htm$' => '/content/$matches[1] [NC,R=301,L]'); 
return $new_rules + $rules; } 
  • Put in your functions.php file, and hook it to the init hook. – Rutwick Gangurde Oct 6 '11 at 5:59
  • Modified the question above to show you what I have now tried based on your suggestion. – Random Oct 7 '11 at 2:02
  • Yes... use functions.php inside your active theme's folder. – Rutwick Gangurde Oct 7 '11 at 2:50

add_rewrite_rule is for converting a URL structure to query vars and routing requests through index.php, you can't point to other URLs and use flags like you would in htaccess rewrites.

for what you're trying to achieve, doing it via htaccess is the simplest way. your rule should work if you just remove the leading slash:

RewriteRule ^content/([^/]+)/([^/]+)\.htm$ /content/$1/ [NC,R=301,L]

This would need to go before any WordPress rewrites if you're using pretty permalinks, otherwise this rule will never be reached.

  • I've attempted to make this the first rule after "RewriteEngine on". What else is in the .htaccess file looks very common & standard. Nothing specific to my site like is set up in the plugins is in here. Should I be looking for a different .htaccess file?? I know it gets processed, because if I do a simple Redirect 301 line it works fine. – Random Oct 7 '11 at 3:56
  • It was the wrong .htaccess file. – Random Oct 7 '11 at 4:05

If possible Can you please show your .htaccess code so we can see that what exactly problem you are facing.

But first of all check whether your mod_rewite module is enabled or not in your web server. To find out it is enabled or not you just put a file in your webserver name phpinfo.php

put this Code phpinfo.php file

<?php phpinfo(); ?>

once this file execute you will see under heading apache2handler in table see Loaded Modules and find mod_rewrite module is present or not. If not present then contact to your hosting provide and make it enable.

If it is enabled then check your .htaccess file once again may be you are missing something.

  • Okay....RewriteEngine on RewriteRule ^/content/([^/]+)/([^/]+)\.htm$ /content/$1/ [NC,R=302,L] – Random Oct 7 '11 at 2:11
  • There is no "Loaded Modules" list on that page. I had already spoken to the ISP and they tell me mod_rewrite is a part of the core. Plus, there are other plugins installed that use mod_rewrite for their own purposes and are working. – Random Oct 7 '11 at 2:23
  • Edit to the above, I'm trying a 301 redirect, not 302. – Random Oct 7 '11 at 2:24

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.