User970008
User970008

Reputation: 1155

PHP Redirect / Get URL and Query String

I have moved the home directory of a site to a subdirectory, so that the sub is now the root.

I still have links in the world pointing to the subdirectory though, so I'd like to do a redirect while grabbing any and all query string parameters.

The news blog used to be at "/news/", but now the blog is the home directory.

For example, I want to redirect

example.com/news/?p=1092
to
example.com/?p=1092

The problem is that there are many query strings, and I want to grab them all.

I can't do: ($_GET["p"])

because there might also be:

example.com/news/?page=1092&location=xyz

I don't think I can use htaccess either because I am still using the news directory in the new application. So the rewrite rules would be very complicated.

I was thinking maybe I could create an index.php in the news directory and do a parse_url or $_SERVER['REQUEST_URI']; and do a split on example.com/news/ but I don't know how.

Thoughts?

Upvotes: 1

Views: 5315

Answers (2)

JimmyBanks
JimmyBanks

Reputation: 4688

Get the URI of hte page

$request_url=$_SERVER['REQUEST_URI'];

output should be news/?p=1023

Explode the results

explode("/", $request_url, 2);

get the components after /news/ and then redirect

$link = $request_url[2]; //the second piece of the explode
header( 'Location: http://www.example.com/$link' ) ;

Upvotes: 1

rjz
rjz

Reputation: 16510

You're looking for the front controller pattern. Google has loads of examples, including this one. Hope this helps!

Upvotes: 0

Related Questions