Why is my mod_rewrite not working?

Ok I have a WP site using the permalink structure of /%category%/%postname%/

I have built it in the way of page templates, using category queries inside, i.e.

Read More

page-help.php

<?php $my_query = new WP_Query('category_name=help');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <!--content-->
<?php endwhile; wp_reset_query(); ?>  

page-about.php

<?php $my_query = new WP_Query('category_name=about');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <!--content-->
<?php endwhile; wp_reset_query(); ?>   

Pretty simple. The problem I have is, I need an archive by year for the news section. I did this by just making the archive.php have the same layout as page-news.php and just query the posts in the news category. *-this is fine in my case because news is, and will only ever be the content to be archived.

My news category (latest-news) is a child of the about category (about), so when I go to the news section, the url is:

www.example.com/about/latest-news/

On the news page is use the following code to list the archives;

<!-- Gets archive for news-->   
<?php $my_query = new WP_Query('category_name=news_article');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <?php wp_get_archives('type=yearly'); ?> 
<!--Ends archive for news-->
<?php endwhile; wp_reset_query(); ?>   

The links it generates naturally lead me to

www.example.com/2000

www.example.com/2001

etc. I want the rewrite to change them to

www.example.com/about/latest-news/2000

www.example.com/about/latest-news/2001

I have modified the .htaccess in the WP route to this:

# BEGIN WordPress  
<IfModule mod_rewrite.c>     
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]    
RewriteRule  /([0-9]+/?)$  /about/latest-news/$1     [NC,L]  #Added this line
</IfModule> 

But I am having no luck, the url remains as

www.example.com/2000

I have the issue of not knowing wether my re-write is wrong, wether I should be putting this line of re-write in a different htaccess elsewhere, or if WordPress is overwriting it.

Any help would be really appreciated.

Related posts

Leave a Reply

2 comments

  1. First, your additional line comes too late. The rule before catches everything you want to match.
    Second, it doesn’t what you want. Your …

    RewriteRule  /([0-9]+/?)$  /about/latest-news/$1     [NC,L]
    

    … matches requests like example.com//0000000000000000000/ or example.com/about/latest-news/2010/ (infinite loop!).
    The first argument for RewriteRule omits the starting /.

    To match year archives you need rather:

    RewriteRule  ^(dddd)/?$  /about/latest-news/$1     [NC,L]
    

    I’m not sure if you really need mod_rewrite for that. Try the following line above the rewrite block:

    RedirectMatch Permanent ^/(dddd)/?$ /about/latest-news/$1
    

    You have to tell WordPress about your custom archive permalinks. You should find enough good examples under the tag.

  2. If you use WordPress without mod_rewrite (docs), all urls are like myblog.com/index.php?p=12. With mod_rewrite you can create pretty urls, like myblog.com/mypost. How this works is that Apache internally rewrites the url myblog.com/mypost to myblog.com/index.php?p=12, before handing the request over to WordPress.

    So mod_rewrite is used to create pretty urls for your blog, but it will not rewrite the links WordPress generates for you.

    To solve your problem, you shouldn’t have to change the mod_rewrite rules.