views:

97

answers:

2

I have a online store that, for example, is located here: hxxp://domain.com/store/

Within the store directory's .htaccess file I have this:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]

It works great!

My issue (problem) is that I now need to change the /store/ directory to /shop/ which seemed simple enough. However, I need to setup proper 301 redirects so that I don't loose any SE rankings that I may have.

What is the best way to setup 301 redirects in this situation?

The only solution I have found is to setup a redirect 301 for each category, product etc. in the store. Like so, for example.

Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/

This works and does what I need it to, but... the URL in the address bar displays like so: hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-widgets

I can not figure out why or how to remove the query string.

Please help. Thanks.

A: 

You can handle the 301 errors by using a PHP script to handle the redirects.

In your .htaccess file you would add this rule:

Redirect 301 /error301.php

Create the file error301.php:

<?php

$redirects = array('/path/to/old/page/' => '/path/to/new/page/',
                   '/store/category/sample-widgets/' => '/shop/category/sample-widgets/');

if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
{
    $dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
    header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
}
dekko
A: 

You could use a simple Redirect directive like:

Redirect 301 /store/ /shop/

Or, if you want to use mod_rewrite as well, you would need to change your current rules as you can not use the base URL /store/ anymore:

RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
RewriteRule ^shop/?$ directory.php
RewriteRule ^shop/search/?$ shop/search.php
RewriteRule ^shop/category/([a-zA-Z0-9!@#$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^shop/([a-zA-Z0-9!@#$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
Gumbo