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
Solved

redirecting domain alias with htaccess

Posted on 2012-03-20
8
433 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 109

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 109

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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