URL Redirection

Previously, I had installed WordPress into a sub-directory called “/blog”. It seemed like a good idea at the time in case I wanted to build a website around the blog. But WordPress ended up being the entire website CMS and I never needed more than WordPress had to offer. So when I moved the website to Bluehost I decided to install WordPress to the root directory. But this meant all the old URLs would break since they included the blog directory as in http://osquest.com/blog. I didn’t want those links to be broken.

The answer was a “301 Redirect”. The 301 is a code that tells search engines and others that care about such things that the redirect is permanent and they should forget about the old URL. (A 302 code means the redirect is temporary and the old URL will be valid again soon.) So… I created a /blog directory on my new site. Then I create a .htaccess file in the directory that has the following line:

redirect 301 /blog http://www.osquest.com

Now, when anything comes looking for the blog URL it will get pointed to the root directory of the site and they’ll find the page there. This does require that the file and directory structure of the new location be the same as the old location, and in my case it is. Everything below the /blog directory was moved to the root directory. [Updated 4/3: While this worked for URL’s I typed into the browser the Google search engine crawler seems to be having a problem with the /blog entries. The .htaccess file probably needs to be modified in the root directory of the site.]

I needed to account for two other changes and the answer was also redirection. I had previously addressed my site as “osquest.com” but now I wanted to change this to “www.osquest.com”. I also wanted to enforce that all URLs would be addressed with the “www” even if no one typed them or if I had linked a page using a url without them.

I addition, The OS Quest was now on a server with other domains. While it could be addressed as www.osquest.com it was also accessible as a sub-domain of the main domain, as in www/maindomain.com/osquest. I didn’t want this. In short, I only wanted it seen as www.osquest.com.

The answer to these last two problems was to modify the .htaccess file in the root directory of www.osquest.com. So I added the following lines to the top of the .htaccess file (all the existing lines were added by WordPress so I put these above those):

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^osquest.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.osquest.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^osquest.com [NC]
RewriteRule ^(.*)$ http://www.osquest.com/$1 [L,R=301]

The first two RewriteCond statements address the subdomain issue while the third addresses the lack of “www”.

Information on the 301 redirect and the www/non-www redirect can be found at Webweavers which is where I found the clearest description. The information on redirecting the subdomain was in the Bluehost knowledge base.