Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-03
9
Medium Priority
?
294 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
8 Comments
 
LVL 111

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 111

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

773 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