One Permalink Only

May 30, 2005
7:24 pm
Posted in: Tech, WordPress

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 http://site.com/2005/04/03/title-of-post/ and people link to http://site.com/2005/04/03/title-of-post (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 http://site.com/index.php into http://site.com/ 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') . '/');
exit();
}
	
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']);
exit();
} 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']);
exit();
}
Mark Jaquith

Hi. I’m Mark Jaquith (JAKE-with). I make WordPress, a free and open source publishing platform and I work as a freelance WordPress consultant. This is my personal blog. You can subscribe to my feed or follow me on Twitter and Google+.

21 Responses

Comments temporarily hidden. Will unhide once I get the spam under control.