Web Development

WordPress style URL rewrites

January 25, 2012 by Mushy in Web Development, Work with 0 Comments

In work I was tasked with writing a very simple article system. It had to archive and have SEO friendly URLs with a simple TinyMCE powered add, edit and delete admin system which allowed image uploads. Since search engines love WordPress sites I decided I’d replicate their URL structure a bit. I didn’t want articles prefixed with the date but its not a hard thing to add in. I basically had to make this structure:

/news/ for the main news listing.
/news/page/2/ for pagination of the main news listing.
/news/year/month/ for an archive link.
/news/year/month/page/2/ for an archive link with pagination.
/article/seo-friendly-url/ for an article.

We weren’t bothering with categories since its a simple article system, although catering for /category/category-name/ and /category/category-name/page/2/ isn’t exactly hard to add to the .htaccess rewrites.

So what I ended up with is below. This might not be perfect but it worked and didn’t affect anything else on the site it was going on (the site will be having a full SEO overhaul shortly but doesn’t have it now so we can’t do any generic URL rewrites for all pages.)

This will also make sure there is a trailing / on the end of URLs so if you type /news it will 301 redirect to /news/ etc.


RewriteEngine on

#Just news page no filters
RewriteRule ^news/$ list.php [NE,L]
RewriteRule ^news$ /article_system/news/ [R=301]

#Pagination only
RewriteRule ^news/page/([0-9])/$ list.php?page=$1 [NE,L]
RewriteRule ^news/page/([0-9])$ /article_system/news/page/$1/ [R=301]

#Archive - year, month and pagination
RewriteRule ^news/([0-9]{4})/([0-9]{2})/page/([0-9])/$ list.php?year=$1&month=$2&page=$3 [NE,L]
RewriteRule ^news/([0-9]{4})/([0-9]{2})/page/([0-9])$ /article_system/news/$1/$2/page/$3/ [R=301]

#Archive - year and month
RewriteRule ^news/([0-9]{4})/([0-9]{2})/$ list.php?year=$1&month=$2 [NE,L]
RewriteRule ^news/([0-9]{4})/([0-9]{2})$ /article_system/news/$1/$2/ [R=301]

#Article
RewriteRule ^article/(.*)/$ article.php?id=$1 [NE,L]
RewriteRule ^article/(.*)$ /article_system/article/$1/ [R=301]

About Mushy

I am Colin Charlton, also known as Mushy. I develop websites, ignore my blog and play games. I also have a kickass tattoo. I should really write more here and something more professional… fuck it.

Leave a reply

Default Secondary Column 2
Default Secondary Column 3
Default Secondary Column 4