Solved

redirecting domain alias with htaccess

Posted on 2012-03-20
8
426 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
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 108

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
 
LVL 108

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

896 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

15 Experts available now in Live!

Get 1:1 Help Now