Solved

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

Posted on 2014-09-03
9
275 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:DataDudes
  • 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:DataDudes
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:DataDudes
ID: 40301380
No, these are actually perl scripts that are running. So a .htaccess solution would probably be the best bet?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:DataDudes
ID: 40301421
Thanks, sorry about that. I thought I had marked Perl and not PHP. Thanks again
0
 
LVL 2

Accepted Solution

by:
DataDudes 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:DataDudes
ID: 40309435
Found solution
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 video discusses moving either the default database or any database to a new volume.

708 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

12 Experts available now in Live!

Get 1:1 Help Now