Solved

PHP Script for URL Redirect with parameters

Posted on 2012-12-26
9
341 Views
Last Modified: 2013-03-20
Hello,

I need to redirect all requests to one domain and redirect it to another.  However, I need it to redirect with all the parameters.

I can put a php page on a Netfirms.com domain.

I have a main domain (e.g. www.LongDomainName.com) and a short domain (e.g. www.shrtDm.com).  

I have recently moved my corporate website into a fully hosted environment.  Previously, we hosted the proprietary CMS.  In my setup, I had IIS forward all requests for www.shrtdm.com to www.LongDomainName.com, including all parameters after the domain.

So  IIS would redirect requests for http://www.shrtdm.com/contactus to http://www.LongDomainName.com/contactus.  It was a wildcard redirect, so it would work for any URL, not just predesignated ones, such as:

Redirect request for:

www.shrtDm.com/page.aspx?pid=10321 

to:

www.LongDomainName.com/page.aspx?pid=10321


Thank You.
0
Comment
Question by:dauyeung
  • 6
  • 3
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38721249
You can use PHP header("Location") to do this redirection.  I'll show you an example as soon as I can find it in my library.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38721264
This ought to do it.
<?php

// GET REQUEST URL + VARS
$path = $_SERVER["REQUEST_URI"];

// SET NEW DOMAIN NAME - FULLY QUALIFIED
$name = 'http://www.MyLongDomainName.com';

// CREATE THE NEW URI LOCATION WITH THE NEW DOMAIN + REQUEST DATA
$uri = $name . $path;

header("HTTP/1.1 301 Moved Permanently");
header("Location: $uri");
exit;

Open in new window

0
 

Author Comment

by:dauyeung
ID: 38722433
I'm new to PHP, I'm guessing that you missed the ?> at the end.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38722548
Good point, but no, I deliberately omitted the close-PHP tag.  The ?> at the end is useless and should be avoided if at all possible.  The only permissible use of the close-PHP tag is when the PHP script must stop so HTML can start.  In professionally written PHP applications this happens very, very rarely.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dauyeung
ID: 38722605
Great.  Thanks.  It works, if I directly request the URL.  ie.  www.shrtdm.com/index.php?pageid=1929

But it doesn't work for other pages, e.g. www.shrtdm.com/index.aspx?pageid=1912.

Any ideas?  I'm thinking that it's more to do with the host setup than anything.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38722646
There is a difference between PHP pages and ASPX pages, and this question was only posted in the PHP Zone.  Sorry but I reflexively responded with only the basic PHP solution.  Maybe you can ask the EE moderators to add it to other EE Zones.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38999416
@Dauyeung:

Why did you give a bad grade?  Why didn't you follow the recommendation to get a moderator to help?

I gave you a tested-and-working code example based on all of the information you provided in the question.  Please see the grading guidelines here and please explain the grade as required by the guidelines.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

Thank you.
0
 

Author Comment

by:dauyeung
ID: 38999519
Sorry.  According to the page you described, you do deserve an Excellent.  Do you know the best way to change the rating?  Looks like I need to post another request.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39001660
You can use "Request Attention" if you want to ask a moderator for assistance. The link is typically near the original question.  Thanks for your consideration.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

18 Experts available now in Live!

Get 1:1 Help Now