Solved

Redirect website subfolder to new site while keeping the URL arguments intact.

Posted on 2014-09-03
9
276 Views
Last Modified: 2014-09-08
Hi,

I have a subfolder on an old website that I would like to redirect to a subfolder on my new site. The trick here is that some people will be going directly to that folder at http://olddomain.com/folder1/ and they need to be directed to http://newdomain.com/folder1/ and there are people who have been emailed URL's with embedded usernames and passwords that need to be directed as well. For example, http://olddomain.com/folder1/cgi-bin/script.pl?name=NE&pw=TEST&psid=XXXXX which would need to redirect to the same URL string at http://newdomain.com/folder1/......
Each person would have their own name=   pw=   and psid=   so I need a way to accept arguments in the redirect.

How can I accomplish this?

Thank you
0
Comment
Question by:OAC Technology
  • 5
  • 3
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40301138
You can use the PHP header() statement to redirect.  You just copy the request variables over and use header("Location:...");  You might also be able to redirect by using Apache .htaccess, but the advantage of using the PHP script is that you have access to the server database and can keep records about the redirection.

You can also read from the other resource without a redirection, but that is a subject for another question.

This is untested code, but probably right in principle.
<?php // demo/redirect.php
error_reporting(E_ALL);

// THE TARGET URL 
$url = 'http://new_web_site.com/path_to_script.php';

// THE GET REQUEST
$get = '?';
foreach ($_GET as $key => $str)
{
    $get .= $key . '=' . urlencode($str) . '&';
}

// TRIM OFF THE TRAILING AMPERSAND
$get = rtrim($get, '&');

// IF THERE ARE NO GET ARGUMENTS, TRIM OFF THE QUESTION MARK
$get = rtrim($get, '?');
$url .= $get;

// SEND THE BROWSER THERE AND TERMINATE THIS SCRIPT
header("Location: $url");
die();

Open in new window

0
 
LVL 2

Author Comment

by:OAC Technology
ID: 40301156
Thanks for the response. How do I use this PHP script if a user is going to, for example, http://olddomain.com/folder1/cgi-bin/script.pl?name=NE&pw=TEST&psid=XXXXX   and needs to be redirected to http://newdomain.com/folder1/cgi-bin/script.pl?name=NE&pw=TEST&psid=XXXXX 
I just want to make sure I'm following correctly

Thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40301363
Hmm... I'm becoming a little concerned about the .pl suffix on the web pages.  Are these PHP pages?
0
 
LVL 2

Author Comment

by:OAC Technology
ID: 40301380
No, these are actually perl scripts that are running. So a .htaccess solution would probably be the best bet?
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 108

Expert Comment

by:Ray Paseur
ID: 40301412
I'm not conversant in Perl, so I'll sign off now.  You can also do the redirect by throwing a <meta refresh> tag into the browser output stream.  So long as you preserve the URL arguments, that will work fine.  I'm pretty sure an Apache .htaccess solution could work, too.

Good luck, and if nobody offers up anything we can mark the question "Neglected" to make E-E poke some of the community experts.
0
 
LVL 2

Author Comment

by:OAC Technology
ID: 40301421
Thanks, sorry about that. I thought I had marked Perl and not PHP. Thanks again
0
 
LVL 2

Accepted Solution

by:
OAC Technology earned 0 total points
ID: 40302085
Figured it out with a .htaccess file. This is all I needed to get everything to work:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [L,R=301]
0
 
LVL 2

Author Closing Comment

by:OAC Technology
ID: 40309435
Found solution
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nameserver and MX Record 2 63
Perl File::Find alternative 1 52
Public IP Address Amazon Servers 2 45
http to https 3 45
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

25 Experts available now in Live!

Get 1:1 Help Now