WordPress: How to do 301 redirects when the old URLs are index.php?variable=something
I recently had the opportunity to take over an old website for a client that was ranking pretty well for his targeted keywords. The old website was built using ModX, a CMS with which I’ve had limited experience. The developer of this site chose not to use mod rewrite to make search engine friendly (SEF) URLs, so all of the pages on the website ended with /index.php?id=123. If the website had used SEF URLs, I would have done my usual list of 301 redirects on the .htaccess file to properly forward users to pages on the new website. I quickly found out, that because of the way WordPress uses the index.php file, this way was not an option. It ends up causing an endless redirection loop as it redirects only the index.php of of the address and ignores the variable, so it essentially tries over and over to redirect to itself.
If you’ve ever looked at the index.php file of a WordPress installation, you know that there’s not much to it. It basically points to the header.php file and the chain of events starts from there that make any given page of your website. I decided I would inject my code just before that process starts.
First, I used $_GET to grab the variable from the URL and make a constant one of my own. Next, I use a series of if/then statements to assign different 301 redirects based on what the variable is.
Between these 301 redirects and the new site map submitted via Google Webmaster Tools, the updating of the URLs in Google indexing should be complete within a few weeks.