WordPress Permalinks: Add /blog/ before blog posts & related page URLs

2011
Jun
18

One of the things I did with this website, and something I see a lot of people wanting to do when they use WordPress as full Content Management System, is to add /blog/ into the URLs of their blog posts and other related pages. While keeping WordPress in the root of their server, and keeping their static pages from having the blog prefix. As an example, you will see the URL of this post as starts off with www.dvorakdesigns.com/blog/, where as my other website pages do not have /blog/ in them.

Really it’s not that hard. But, if you’re picky like me, you not only want your blog posts to have the URL prefix, but also your category, tag, archive and author pages as well; pretty much anything related to the blog portion of your website. This proved to be a little more difficult to figure out, though I eventually did thanks to help from people over at WordPress StackExchange.

As a quick note/warning, this will not work if you have a sub-directory on your server named “blog” or if you have a sub-domain (which would make a sub-directory) on your server at blog.yourdomain.com. Also, if you’re like me and used to have a blog at a sub-domain like that, you will have problems setting up redirects. I’m currently looking into a solution for that now, but no one seems to know how to do it. Once I figure it out I will post it.

At any rate, here’s how to setup the URL structure.

First, you will need to setup WordPress to use a static page for your homepage, and a different page for the index of your blog (the page that shows all your recent posts). If you’re using WordPress as a CMS, you’ve probably already done thing, but in case you haven’t, here’s how to do it.

Create two new pages. One will be your new, static home page, and the other will be the index for your blog. The page you make for your homepage can contain any content you want, but the page you create for your new blog index should be left blank. Also, in order to keep the consistent URL structure that we’re trying to create, be sure the URL slug for your new blog page is just “blog.”

Once you have your pages created, under the Settings header on the left hand menu, click on Reading. On this page, you will see the Front page displays option. Select the A static page option, and pick the two correct pages from the drop down menus. If done correctly, your blog index should now reside at a URL like www.example.com/blog.

Next, you will need to edit your .htaccess file to remove the /category/ prefix from all of your category pages. By default, WordPress will make your category URLs look like this: http://www.example.com/category/category-name. We want them to look like this (for now): http://www.example.com/category-name.

To accomplish this, add the following line into your .htaccess file. Be sure to keep a backup of your .htaccess file, and don’t overwrite anything that’s already in there. Also, be sure to change the website URL.

RewriteRule ^category/(.+)$ http://www.your-website.com/$1 [R=301,L]

After that, go back to your WordPress admin panel again. On the left hand menu, under the Settings heading, click on Permalinks. This will bring you to the page where you can edit your permalink structure. You will want to setup your permalinks as follows.

Select a Custom Structure for your common settings, if you haven’t already, and set it up as /blog/%postname%. This will make your blog post URLs look something like this: www.example.com/blog/article-title. If you had something for a Custom Structure already, just add /blog/ before it.

For Category base, put in /blog/category. This will make your category links look like this: www.example.come/blog/category/category-name. The reason we had to first remove the category prefix using the .htaccess file, is to prevent WordPress from adding the prefix where we didn’t want it. Without first removing it, our URLs would have looked like this: www.example.com/category/blog/category/category-name. While I don’t think it’s supposed to happen that way, I did run into that problem, so first removing it to give us a clean slate was the easiest safeguard.

Finally, for the Tag base, add /blog/tag. As you can probably guess by now, this will give you a URL that looks like this: www.example.com/blog/tag/tag-name.

You should now have a nice, organized URL setup for your WordPress-based website. If you run into any problems, don’t hesitate to leave a comment.

Did you enjoy this article? Subscribe to our RSS feed, bookmark this post for later, and share it with your friends! Use these handy buttons...
RSS Feed Digg Reddit StumbleUpon Mixx Technorati Delicious Facebook Twitter ShareThis

About the Author



Visit Preston Dvorak's Website Follow Preston Dvorak on Twitter
Preston Dvorak

I've been working on website design and development since I was 12, and professionally for two years now. I continually work on learning the newest techniques and standards for web development, and enjoy the challenges that each website brings. This blog is my way of giving back to the community while sharing my ideas and knowledge that I pick up along the way. I hope to continue to improve on the articles I write and the topics I cover. Thank you for reading!

Posted in in Coding Tutorials

Want to be a guest poster on our blog? Find out how to submit your article and reap the benefits.

Related Posts

Move in chronological order instead...
  • nick

    Awesome article. It is exactly what I was looking for! 
    While everyting works when you have a default blog (with posts and pages), if you try to add a custom post type the custom post type also goes under the /blog. This is the problem I am messing with for many days. Can you please give me any idea on how to work on this? Have you experienced anything like this?

  • mastif ron

    Everything is a very clear explanation of concerns. Data actually. The site is really useful. Thank you very much for sharing.
    california seo

  • Eric Downing

    You can just change the permalink structure to /blog/%year%/%monthnum%/%day%/%postname%/
    Anything after the blog can be changed

  • http://dezayo.com/ dezayo

    as for me, this article is nice
    href=”http://dezayo.com/”>dezayo

  • Vegard Giskehaug

    Great article, everything works, except for the images, they get this kind of link: site.com/blog/wp-content/etc… while the image is in the site.com/wp-content folder….

    Any idea?

    Thanks!

  • Strigkos Nomikos

    Excellent!!!

  • http://vitaltechnical.com/ Vital Technical

    Good tips! Thanks for sharing!

  • http://www.phasechangedigital.com/ Ian

    If you leave category base blank and add “/blog/” to the custom permalink you won’t have any need to customize the .htaccess file. In fact, you probably shouldn’t because there are several plugins that end up overwriting .htaccess for their own purposes…if you have any of those, you’ll have to add it back in every time you make a change…

  • Samiul Sheikh Jahid

    Web Development Company

    ASEUM INFOTECH is a global software services and IT
    management company. We pour our love into the things we build, take pride in
    the quality of the code, and measures success by customer satisfaction and
    happiness. Quality,Reliability and Safety.

    Go
    to:>>> http://www.aseuminfotech.com/