Redirecting a Folder to a Subdomain using .htaccess
As loyal visitors may notice we recently moved our blog from the http://www.jjtcomputing.co.uk/blog/ folder to the http://blog.jjtcomputing.co.uk/ subdomain. This was relatiely simple to acheive but now the number of visitors seeing a 404 Error when looking for the old URLs has rocketed, as search engines and linking sites have not yet re-indexed us.
So we had to do some redirecting using a .htaccess file. This is a file that goes in the root of your webserver and the root of any subdmains. It handles access, error pages, and custom Apache features.
To redirect all files from a folder to their file, use the following code in your .htaccess file in both the webserver root and the subdomain-to-be.
Options -Indexes +FollowSymLinks
RewriteEngine On # only needed once in any htaccess file
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/blog/(.*)$ http://blog.example.com/$1
Replace 'example.com' with your domain name and 'blog' with your folder name. The 'R=301' tells spiders/bots that this is a permanent redirect
- Apache /
