Solved

redirecting domain alias with htaccess

Posted on 2012-03-20
8
435 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 110

Expert Comment

by:Ray Paseur
ID: 37742040
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
ID: 37742053
Thanks Ray_Paseur for your reply.

The solution needs to done via htaccess.

Thanks.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742147
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
Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742212
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
 
LVL 15

Accepted Solution

by:
babuno5 earned 400 total points
ID: 37744537
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
ID: 37744927
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
ID: 37745106
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
ID: 37745900
Thanks everyone for your help.
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

733 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