I've just updated a website that used .php urls, to a Wordpress 3.1 site, that uses no file type extentions, (custom permalinks).

They have a lot of references in their imported content to an old page ending in .php which now goes to an error 404 page.

My question is this: What is the rewrite rule I should use in my htaccess file that would redirect someone clicking on a link to www.somesite.com/glossary.php#anchor, to www.somesite.com/glossary/#anchor?

Any help gratefully appreciated, S.

2 Answers 2


Your first order of business should be updating the links in your imported content so that people aren't given links to the old URLs in the first place. This should be pretty easy to do with an SQL replace. For your situation, it'd be something along the lines of:

UPDATE wp_posts SET post_content = REPLACE(post_content, '.php', '/');

You gotta be realy careful with find/replaces though, as they sometimes replace things you didn't anticipate. It's a good idea to back up your DB before doing anything like this.

As for an htaccess fix: it's definitely a good idea to do 301s for the old URLs if you have a lot of inbound links, which translate to SEO value. But the amount of work you put into this should be proportional to the value of those inbound links.

I'm thinking it's not a good idea to try to rewrite ALL .php requests, as Wordpress often needs to make legitimate ones. For example, all requests on the frontend are routed through index.php, and when I tried setting up a rule like the one you asked for, it broke WordPress altogether. Know what I'm saying?

An alternative is to set up individual rules for each page you want to 301. Of course, this might be a bad idea if you have a LOT of links--both because it's a hassle, and could impact performance.

Hopefully someone else has a tricky htaccess workaround for you, but I'm stumped.

  • Mant thanks - didn't even think of the mysql replace approach - worked a treat!
    – sta777
    Apr 14, 2011 at 8:42

As far as I know, the hash at the end of the url (#anchor here) never gets sent to the server. However, if you can just get WordPress to load the correct content when glossary.php is requested, the anchor should work just the same.

MathSmath is right about 301 redirects being important, but I'd suggest running this stuff in WordPress rather than through .htaccess.

Specifically, I'd recommend using the Redirection plugin. It gives you a simple to use GUI to manage all your redirects that WordPress doesn't take care of automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.