I immediately want to apologise to the authors of these plugins but the fact remains, sometimes someone comes along offering to kill a flea with an elephant gun, and well… it’s time to just come right out and admit that was the wrong approach.
The two offending plugins on my hitlist for today are the Permalink Redirect plugin, which Ronald Heft (cavemonkey) and others have listed as a Top Ten WordPress plugin, and the no-www plugin, by no less a luminary than Matt himself (aw crap can you get away with contradicting da man?!).
Both these plugins do with an elephant gun what Apache can do with a pea-shooter. Yep… I’m speaking again of mod_rewrite and a .htaccess file. My point is simple enough… use the right technology for the right problem; always keep things as simple as possible.
A quick and obvious point: if you can’t use mod_rewrite because your host doesn’t turn that on in Apache… get another host. If you’re on IIS you can get a host to achieve something similar with an ISAPI filter (that’s really pretty cool, and one I’ve stress tested under load and checks out OK), but it’s fair enough that many Windows Server hosts will need some convincing to install that. But… if you’re genuinely left in this “can’t use mod_rewrite camp” then I guess the plugins are fair enough. What else can you do?!
But now for the rest of us…
I’ve already posted about how to use mod_rewrite to forcibly include or exclude a www in your domain (URL). You’ll notice on my site that if you remove the www, it gets stuck back on. So we’ll move on.
And the other plugin? Cavemonkey is absolutely right in explaining the importance of the function of this plugin:
One of the fatal flaws of WordPress is that URLs with a trailing slash are the same as URLs without. This is bad for your stats since one page can show up under multiple URLs, and it’s also bad for search engines who may duplicate your pages in their results.
No arguments there. Just a question of how it’s done. Here are the two lines to add to your .htaccess file to achieve the same thing in a far simpler, lightweight kind of way:
RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
I add this mod_rewrite rule under the one that deals with the “www” and together they sit above the standard WordPress rules, and look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.alistercameron.com$ [NC]
RewriteRule ^(.*)$ http://www.alistercameron.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
</IfModule>
It’s not rocket science, and it does assume you can edit a file via FTP, but that’s all there is to it.
If you get something wrong, yes, mod_rewrite can kill your site in one fell swoop, but if you quickly restore the earlier version of the file and check your changes you’ll be fine.
Don’t you feel so much better now? We’ll leave the elephant gun for another day… After all, it was just a couple of fleas ![]()







20 Comments
you were wondering... I believe in rewarding commenters!
Here is a challenge for you
I am not a .htaccess guru, but after reading the thread over at Aaron Wall’s blog recently, and especially the comments, I decided it must be possible in some way to create a universal .htaccess file that I could upload to 50+ blogs and for it to work immediately.
Is there a reason http_host can’t be used for removing www from URLs ?
Also for trailing slashes, why would your code work better than this example which I picked up in the same thread and comments?
# If requested resource does not exist as a fileRewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
RewriteRule (.*) $1/ [R=301,L]
I am just looking for something universal I can just upload and it works, which is the advantage sometimes of plugins.
Andy, can do.
# If subdomain www exists, remove it firstRewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
… should do the trick. You’ll need this as your first rule, ideally.
Let me know how it goes. At the end of all this I’d like to post the “ideal generic” .htaccess file for WordPress users… if it makes sense to do that and if we’ve really added some value here
So… Did you ever come up with the ideal generic .htaccess file for WordPress?
Brilliant. Sadly, I am not brilliant, lest I would know how to work this myself. The one thing that this solution does not appear to do which the permalink redirect did is convert ?p=# into the permalink on the fly. I send out ?p=# type links to publicists because my regular permalinked URLs can get a little…long. And it beats having to go to tinyurl. Am I simply up a creek on this one?
@Widgett Walls — yes there is a plugin to do that automatically, and it is called “Permalink Redirect”. It can automatically redirect /?p=# style URL into full URL defined by your permalink structure.
Oh wait. Sorry. I have also just noticed that this is the WordPress plugin mentioned in the article that you don’t and shouldn’t use
Scott: Story of my life: It’s what you need! But it’s bad for you! But it’s what you need! No, it’s bad for you! Whether chocolate, caffeine or WordPress plugins, in the end it’s all the same.
The problem with these rewrite rules is they don’t work if your blog is not in the root. ie.
www. example.com/blog
becomes a mess under these two rules
The problem is I can’t quite make it work with both removing the www. and adding the trailing slash. Either one works and/or one breaks the other.
Added bonus for removing index.php if someone tries to do example.com/blog/index.php
How about if I *don’t* want the trailing slash?
I poked and prodded around in my .htaccess a bit but all I got was bad redirecting in an infinite loop.
Oh, and nice all-domain no-www code. I’ve been looking for something like that.
Alister this page looks bad
I thought you may need to know.
Weird.. now it looks great but a minute ago was full of database errors..
My bad.
You are so right, i wish they would start integrating it into the base of WP sooner rather than later ya know..
@ http://www.askapache.com/htaccess/apache-htaccess.
Thank you! Your www redirect worked better than what I had been using, and the force trailing slash works great. I’ve had the worst time getting that working on my site before now. It seems important to put the trailing slash rule before the standard wp rules — you mentioned that’s where you put it, but might be a good idea to emphasize this if you update your post at all.
Thanks again!
Some free hosting sites don’t allow .htaccess to be used. One hosting company I use offers 5GB space and 500GB bandwidth but they have file type rules and .htaccess isn’t allowed with the free level. So the Permalink Redirect plugin is a GREAT.
hey thanks a lot for the redirection code .. i’ve ben trying all da to get it to work on my site..
http://www.discussheroes.com
An old topic, but I have a relevant point, url’s with WWW rank better than those without. I don’t know why…..but the stats show that they do.
Excellent — that’s 2 problems solved. Now why won’t the 301 redirects work with this code? For instance, I want to redirect one specific page to the next, and it goes directly to my 404 page, instead of the intended page.
this is what I have in my htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.clickstomysite.com$ [NC]
RewriteRule ^(.*)$ http://www.clickstomysite.com/$1
[R=301,L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
redirect 301 /AboutUs_Clicks.htm
http://www.clickstomysite.com/about
ErrorDocument 404 http://www.clickstomysite.com/404.html
Totally agree with you Alister. Also don’t use canonical link plugins as they all declare wrong urls to be the canonical one. I tried them all on this site about website hosting and they appear to corrupted.
I am having a weird problem with my blog and I haven’t been able to find an answer, I believe it could be fixed with some editing to the .htaccess file but I don’t know.
when I get a search hit from google the link shows up like so:
http://realecig.com/?/ecigs/dragonfly/
When it should be:
http://realecig.com/ecigs/dragonfly/
There is an extra question mark added to the url which shouldn’t be there. Does anyone know how to get rid of this?
I learn your modification to .htacces. It’s looks simple but powerful.
Yes, you’re absolutely right.. I also don’t use plugins for simple handy things. Currently I’m having a problem with trailing slash. At my permalink (some times for long permalink) the slash is often missing. Every time I can’t check, No idea why it’s happening.. can you suggest any simple trick for this?
28 Trackbacks/Pingbacks
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted -1930 years, 8 months ago
Posted 5 years ago
Posted 4 years, 10 months ago
[...] couple of months ago Alister Cameron posted a simple solution to .htaccess such that you didn't need to use a plugin to convert URLs using www to URLs [...]
Posted 4 years, 10 months ago
Posted 4 years, 9 months ago
[...] processing, but I didn’t expect the results. In my search for .htaccess rules, I came across this post addressing my issue specifically. In fact, so specifically the site even mentioned my name as a proponent of Permalink Redirect! I [...]
Posted 4 years, 9 months ago
Posted 4 years, 9 months ago
[...] I spotted the code and other interesting things at Niche Marketing – Andy Beard which took to me to Alister Cameron // Blogologist where I was able to read even more. Both of these blogs are valuable resources for anyone that [...]
Posted 4 years, 9 months ago
Posted 4 years, 3 months ago
Posted 4 years, 3 months ago
[...] I used my search mojo to see that someone has already described the cure so I won’t have to: Two WordPress plugins you don’t need… and shouldn’t use. Tags: links, off page seo, on page seo, page rank, plugins, pr, search engine friendly, search [...]
Posted 4 years, 2 months ago
Posted 4 years, 1 month ago
Posted 3 years, 5 months ago
Posted 2 years, 11 months ago
[...] trailing back-slash if missing (Can also be done with htaccess or [...]
Posted 2 years, 7 months ago
[...] from Alister Cameron. It works for Linux/Unix and requires .htaccess file access. In the web root directory .htaccess [...]
Posted 2 years, 6 months ago
[...] 来自:Two WordPress plugins you don’t need and shouldn’t use [...]
Posted 1 year, 9 months ago
[...] plugins and widgets Ultimate List of Nofollow Dofollow Plugins The Essential WordPress Plugins Two WordPress plugins you don’t need and shouldn’t use Best WordPress Plugins Nominations and Results wordpress plugins installed here 10 Most Amazing [...]
Posted 1 year, 4 months ago
Posted 1 year, 4 months ago
Post a Comment