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(); }
IO ERROR says
Hey! Removing “www.” is not going to work for everyone, and many people aren’t going to want to do that.
Mark says
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:
[deleted]
Okay, that got butchered horribly, I’ll add it to the
phps
commented out.Mark says
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!
Geof F. Morris says
Yeah, I dig this. [And I’m also no-www. ;)]
Mathias Bynens says
Like I stated earlier, the trailingslashed vs. untrailingslashed IRI thing is just one example. Stuff like
http://site.com/?p=123
would also be redirected to the correct permalink IRI.As for your plugin, great work. Both
www.
andindex.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…)Mathias Bynens says
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));
exit();
}
Also, try to use single quotes where possible when coding in PHP. They’re a couple of milliseconds faster.
Mark says
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.Mathias Bynens says
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));
exit();
}
Michael Benson says
Wouldn’t it be so much easier to just .htaccess mod_rewrite rules; on both counts?
Mark says
Yes. This is for people who can’t (or don’t feel comfortable) doing that.