Solved

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

Posted on 2014-09-03
9
282 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
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…

735 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