Solved

redirecting domain alias with htaccess

Posted on 2012-03-20
8
420 Views
Last Modified: 2012-03-20
Hi Experts

We have old urls in google and other locations but have a brand new domain and site structure. We would like to 're-map' the old domain and urls to their new location.

The old domain is setup as a domain alias of the new domain with the hosting company.

Is it possible with htaccess to redirect:

http://olddomain.com/index.php?page=services
to
http://newdomain.com/subfolderA

http://olddomain.com/index.php?page=contact
to
http://newdomain.com/subfolderB

etc


Thanks for your time.
0
Comment
Question by:hnEE
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You can probably do this with .htaccess or with PHP.  I think either way you want to send a 301 moved permanently header.  I will try to show you a PHP example of how the design might work.
0
 

Author Comment

by:hnEE
Comment Utility
Thanks Ray_Paseur for your reply.

The solution needs to done via htaccess.

Thanks.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Here is the PHP example.  If the list of new URLs is very long, I might be more comfortable using the PHP redirection, since it may be easier to keep it organized.  At least that is the way I would think.  You might add a logging statement to this to keep track of the number of times it was necessary to redirect clients.  Then you would have a sense of when the redirection is no longer needed, ie, when everyone has found the new URLs.

This code is completely untested, but valid in principle.
<?php // RAY_temp_hnEE.php
error_reporting(E_ALL);


// MAP OLD DESIGN TO NEW DESIGN LIKE THIS /*
http://olddomain.com/index.php?page=services
to
http://newdomain.com/subfolderA

http://olddomain.com/index.php?page=contact
to
http://newdomain.com/subfolderB
*/


// YOU MAY WANT THIS OR NOT - NOT SURE
session_start();

// AN ARRAY TO ORGANIZE THE PAGE NAMES AND THE FOLDERS
$map = array
( 'services' => 'subfolderA'
, 'contact'  => 'subfolderB'
)
;

// ASSUMPTION: WE DO NOT KNOW WHERE TO GO
$url = NULL;

// IS THE REQUEST PRESENT AND WELL-FORMED?
$page = isset($_GET['page]) ? strtolower(trim($_GET['page'])) : NULL;
if ($page)
{
    // IS IT A REQUEST FOR A PAGE WE KNOW ABOUT?
    if (array_key_exists($page, $map))
    {
        // SET THE DESTINATION PAGE
        $url 
        = 'http://newdomain.com' 
        . DIRECTORY_SEPARATOR 
        . $map[$page]
        ;
    }
}

// IF WE KNOW WHERE TO GO
if ($url)
{
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: $url"); 
    exit;
}

// HANDLE THE 404 NOT FOUND CONDITION HERE...

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Sorry -- I just assumed since this question was in the PHP zone that you were open to a PHP solution.  Maybe one of the other experts will give you the .htaccess version.  

Best of luck with it, ~Ray
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Accepted Solution

by:
babuno5 earned 400 total points
Comment Utility
here you go

      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC]
      RewriteCond %{QUERY_STRING} ^page=services$
      RewriteRule ^index\.php$ http://www.newdomain.com/subfolderA? [R=301,L]

      RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC]
      RewriteCond %{QUERY_STRING} ^page=contact$
      RewriteRule ^index\.php$ http://www.newdomain.com/subfolderB? [R=301,L]


Hope the above helps
0
 

Author Comment

by:hnEE
Comment Utility
HI babuno5

Thanks for that - perfect!

What should we do though if they come to the homepage:

http://olddomain.com/
to
http://newdomain.com/

I had a go at working this out but it cancels out your other work from above.

Thanks.
0
 
LVL 12

Assisted Solution

by:larsrohr
larsrohr earned 100 total points
Comment Utility
Try this after the previous lines by babuno5:

      RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC]
      RewriteRule ^/(.+) http://www.newdomain.com/ [R=301,L]

Since the other rules have an L, they should be processed as the last rule, if matched. So only those that didn't match will get this new rule.
0
 

Author Closing Comment

by:hnEE
Comment Utility
Thanks everyone for your help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now