Gustavo Campos Gustavo Campos - 4 months ago 9
Ajax Question

how to redirect the page analyzing url typed?

I need a page that when I load it check out the link typed for example:

www.test.com/myphp.php=10


if it finds certain number it redirects to another link for example:

found
www.test.com/myphp.php=10
redirects to news
www.test.com/news/10.php
.

How can I do this and what technology should I use?

Answer

First off, your syntax for GET variables is a little off. I assume you mean starting URLs of test.com/myphp.php?redirect=10 or something similar? Because www.test.com/myphp.php=10 is bad syntax.

I'm assuming we're using GET variables as described above in my answer:


The simplest way to do this would be to just set the location header in PHP:

if(array_key_exists("redirect", $_GET)){
    #set header to redirect to new location
    header("Location: /news/" . $_GET["redirect"] . ".php");
    #don't let the page do anything else
    die();
}else{
    #do something if the GET variable doesn't exist
}

Note that this way introduces a few security vulnerabilities, so you might want to do something more advanced (such as intval the GET variable so that they can't inject a script into your variable, or just addslashes() to the GET variable value).

Comments