One Permalink Only

Thanks to Matt, I found this cool WordPress plugin by Scott Yang that aims to unify your site’s permalinks. Say you have an entry at and people link to (without a trailing slash). That will get indexed by search engines as two different entries, and may even hurt your ranking. The Permalink Redirect Plugin fixes that by sending a 301 redirect to such requests. Very cool.

I have something that will take that one step further. This code will turn into and the second part will enforce your use or non-use of www.

Here it is as a plugin: Enforce www. Preference

And here is the code, in case you just want to integrate it into something else. This could very well go at the bottom of Scott’s plugin, or within the function.

if ( $_SERVER['REQUEST_URI'] == str_replace('http://' . $_SERVER['HTTP_HOST'], '', get_bloginfo('home')) . '/index.php' ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . get_bloginfo('home') . '/');
if ( strpos($_SERVER['HTTP_HOST'], 'www.') === 0  && strpos(get_bloginfo('home'), 'http://www.') === false ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://' . substr($_SERVER['HTTP_HOST'], 4) . $_SERVER['REQUEST_URI']);
} elseif ( strpos($_SERVER['HTTP_HOST'], 'www.') !== 0 && strpos(get_bloginfo('home'), 'http://www.') === 0 ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

21 thoughts on “One Permalink Only

  1. Mark Post author

    Then they can comment that part out. ;-) Or, they can switch it around to insert it if it’s not there, fairly easily, although it should really be done at the Apache level. The point is to standardize it one way or another.

    This should work:


    Okay, that got butchered horribly, I’ll add it to the phps commented out.

  2. Mark Post author

    Even better, I added auto detection. It will consider what you have set as your blog’s URI in WordPress and enforce yes-www or no-www accordingly. Everybody wins!

  3. Mathias Bynens (subscribed)

    Like I stated earlier, the trailingslashed vs. untrailingslashed IRI thing is just one example. Stuff like would also be redirected to the correct permalink IRI.

    As for your plugin, great work. Both www. and index.php are indeed cruft. However, I prefer to do these things through some mod_rewrite rules. (I guess stuff like this could also be made as a WP plugin…)

  4. Mathias Bynens (subscribed)

    How about removing all instances of index.php in IRIs?

    if(strstr($req_uri, '/index.php')) {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . get_bloginfo('home') . str_replace('/index.php', '/', $req_uri));

    Also, try to use single quotes where possible when coding in PHP. They’re a couple of milliseconds faster.

  5. Mark Post author

    Mathias, yeah, I do the no-www thing using mod_rewrite as well, but for people who aren’t comfortable tinkering with .htaccess this is a simple plug-and-play way to do it. mod_rewrite would be faster.

    As for removing all instances of index.php, that’ll break some blogs. For instance, people who have permalinks like /index.php/2005/04/03/title-of-post/ that use PATHINFO would be in trouble, and all pages except for the front page would 404.

    Of course, we could remove /index.php? and replace it with /? for people who aren’t using mod_rewrite rules, but I think Scott’s plugin already does that.

    As for doublequotes vs singlequotes… I usually try to use single. I just copy pasted that header("Location: " part from something else! I’ll fix that.

  6. Mathias Bynens (subscribed)

    Good point. I suppose you could do something like this, then:

    if(!strstr(get_settings('permalink_structure'), '/index.php') && strstr($req_uri, '/index.php')) {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . get_bloginfo('home') . str_replace('/index.php', '/', $req_uri));

  7. Pingback: …it could be worse… » no-www

  8. Pingback: www and trailing slash problem « WordPress Support

  9. Pingback: Literal Barrage

  10. Pingback: Literal Barrage » No Dub Dub Dub Dot

  11. Pingback: Purple Lilacs » Tags, Categories & Permalinks

  12. Pingback: Surpass Community - Permalink Plugins

  13. Pingback: Plugins used at balswing & balboa galaxy

  14. Pingback: [wp-hackers] WordPress 2

  15. Pingback: Products and Advice for Home Business Success | Home Business Solutions

  16. Pingback: Eric Wallace's digital playground |

  17. Pingback: Photo Matt » One True Permalink Plugin

Comments are closed.