1

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?

2 Answers 2

1

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' ) ;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I got it working. I had to set the $request_url = explode('/',$request_url);
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.