Solved

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

Posted on 2014-09-03
9
287 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
[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
  • 5
  • 3
9 Comments
 
LVL 110

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 110

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
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

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

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
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…
Six Sigma Control Plans

630 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