Solved

redirecting domain alias with htaccess

Posted on 2012-03-20
8
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

695 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