How to get rid of /blog/ in permalink or customize it!!!

Ok, I’m very excited to give back to the WPMUDev community after all of the help you’ve given me so far! You guys have been awesome… and I want to be one who gives back… not just takes. :slight_smile: (But if you decide to thank me with a point or two, I certainly won’t mind!)

I’ve been racking my brains on how to get rid of that annoying /blog/ subdirectory that is inserted into permalinks automatically. You see, for those of you who don’t know… this is a rather brainless solution to a problem that comes up with multisite when using the subdirectory instead of subdomain structure… if you have a post called “blah blah” and your permalink is site.com/blahblah/… and then someone creates a new site (because you’re using Multisite) called blahblah… WP will try to create the new site under the dir blahblah under the root (site.com/blahblah). What that means is that you’ll have a conflict.

So rather than try to simply programatically search the db and make sure there are no conflicts and resolving such before creating the new site, the rather lazy solution was “oooh! Let’s just put all the blog content under a directory called blog!”. To me a better solution would have instead create all new SITES in a new subdir (maybe “site”:wink: instead of content… but whatever. I’m sure there’s a reason (other than a brainfart), but I don’t know what that reason may be.

Problem with this is, if you go from a regular to multisite setup… guess what happens to your posts. Yup. They originally pointed to /blahblah… but now it’s blog/blahblah. That can be a problem. I mean, I believe that WP probably automatically redirects it… though so far, my own personal test didn’t confirm that.

Me no likey. And there’s not much out there on the interweb for solutions… you can do a change in super-admin to the permalink structure. But it didn’t work for me AT ALL. There is also a plugin or two… again… no workey. So me no likey.

So, here’s how I fixed it… By the way… make sure the rest of your permalink structure is set up the way you like it before you proceed. (i.e. /%postname%/)

Using phpMyAdmin, I went into the wp_options table. In there are two entries… one with option_name called permalink_structure and the other called rewrite_rules. And then this is what I did:

1. Copy the current contents of the column called option_value. Paste this into a spreadsheet for safekeeping somewhere. That way, if you screw something up, you’ll have what you need to revert to the way it was before you made any changes. I’d also note option_id and option_name as well.

2. Edit the row, particularly the option_value column. Find wherever you see “blog/” and delete “blog/”. For the rewrite_rules row, I found it easier to copy the option_value and paste it into a text editor and do a search and replace. I replaced the “blog/” string with nothing (not a space… just leave the replace value blank). Of course, if this does nothing for you, then just do a find… and then physically delete that string “blog/” wherever you see it. If you do this in your text editor, copy the results and paste the new version back where you just where.

3. Just for kicks, do a commit; statement to make sure the change takes.

That should do it! Yup! Very easy… but just make sure to test it a few times… all your posts should now point to site.com/blahblah instead of the ugly site.com/blog/blahblah.

NOTE: If you ever go into the Permalink settings in your dashboard and make any changes and save them, THIS WILL UNDO WHAT YOU JUST DID (and come to think of it, would probably be a great way to back out if you wish to undo it). So just keep that in mind. If you make this change and want to keep it, don’t make any changes ever to the permalinks settings. If your permalinks are not set up the way you want to, be sure to do so BEFORE you make any changes to your database table.

If you have any questions, let me know in this thread. I’m not an expert and I say to do the above at your own risk… but so far it works for me. :wink:

If you found this post helpful, if you don’t mind… I’d love some points. I’m new here and I want to get to 1000 points someday so I can be an honorary lifetime member. But I know that I have to work hard and contribute alot to get there. I hope that this thread will benefit many!!!

  • Shawn
    • The Crimson Coder

    The fact that saving the permalink structure again overwrites the changes should be a clue to an alternative method. A filter that checks if the blog root begins with “blog/” then strips it out before saving would be the easiest method. The following is vaporcode (which means it hasn’t been tested), but ought to work. The normal caveats apply: buyer beware, caveat emptor, no warranty, guarantee, use at your own risk and all that.

    # vaporcode that should remove 'blog/' from the permalink structure
    function myblogfix( $newvalue, $oldvalue ){
    return preg_replace( /^blog//, '', $nv);
    }
    add_filter( 'pre_update_option_permalink_structure', 'myblogfix', 2)

  • warpedmind
    • The Incredible Code Injector

    By the way, I just realized that I mentioned that you could rename blog/ to something else, if you’d rather do that than delete blog/ altogether. To do so, obviously, just replace the actual string “blog/” with “whatever/” within those same rows in the table mentioned in my first post.

  • warpedmind
    • The Incredible Code Injector

    I hope it helps others, Arun. It looks like many have had this same question.

    And by the way, one thing I read often was the suggestion to set up multisite as a subdomain structure instead of subdir. That is easiest of course… but if you wish to take advantage of the content in the subsites, then you should go about this with the subdir method.

  • Shawn
    • The Crimson Coder

    In the functions file or within a plugin. I use a home-brewed plugin for each site I work on that I use specifically to include this kind of stuff so it doesn’t get lost when I switch themes. If you’re unlikely to switch themes, or won’t forget to copy the code to the next theme, then functions.php is a great place for it.

  • Arun Basil Lal
    • New Recruit

    Say, I have a site on your Multisite domain, lets call it domain.com

    If I have a blog on domain.com/me and if I tell my friends about it, its still domain.com that gets all the attention.

    But if it was me.domain.com. Then that sounds cooler right?

    As in the case of this forum, if it was wpmudev.org/premium and not premium.wpmudev.org, the first one would be more like a folder around here, isn’t it?

    I hope you get my point. It’s a subjective one, as are all other things “cool” :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.