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.


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


<?php $my_query = new WP_Query('category_name=about');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?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:


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



etc. I want the rewrite to change them to



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

But I am having no luck, the url remains as


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.

  • I mis-read your question in my original answer. In your desired destination URL, where are you expecting the phrase "latest-news" to come from? – Annika Backstrom Mar 2 '11 at 12:39
  • @Adam Backstrom I have edited the question to make it more clear. The /about/latest-news section is where the news page is found. I just want the mod_rewrite to add 'about/latest-news/' before a string of 4 numbers in the url. – danixd Mar 2 '11 at 13:19

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  ^(\d\d\d\d)/?$  /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 ^/(\d\d\d\d)/?$ /about/latest-news/$1

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

| improve this answer | |
  • Thanks the Redirect worked. Now I have a new problem, but that is the answer to this question, so thank you for the help. – danixd Mar 2 '11 at 14:06

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.

| improve this answer | |

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.