August 8th, 2008

Using htaccess to remove / redirect a folder in a url

At CampusLIVE, we completely overhauled our codebase which resulted in all of our urls in Google to incorrectly go to our homepage. Here’s an example along with the solution

The Goal
The goal was to redirect any request to CampusLIVE.com/home/(anything) to CampusLIVE.com/(anything) so that all links in Google would work again.

Example of Old Url
http://www.campuslive.com/home/umass/restaurants

Example of New Url
http://www.campuslive.com/umass/restaurants

The Solution
Open up you .htaccess file in any text editor. My code can be substituted to work with your domain/folders.


Options +FollowSymlinks
RewriteEngine on
RewriteRule ^home/(.*)$ http://www.campuslive.com/$1 [R=301,L]

Summary

The above code will successfully redirect permanently any requested to /home/(anything) to /(anything).By using the 301 redirect, you are telling the search engines to permanently use the new address.

Leave a Reply