Gustavo Campos
Gustavo Campos

Reputation: 9

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?

Upvotes: 1

Views: 137

Answers (4)

strwoc
strwoc

Reputation: 533

Plain old javascript

function getQueryVariable(variable)
{
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
   }
   return(false);
}

If the url is www.test.com/myphp.php?id=10 Calling getQueryVariable("id") would return 10

Then you just reroute using

var number = getQueryVariable('id')
window.location = "www.test.com/news/"+number+".php";

Upvotes: -2

Disfigure
Disfigure

Reputation: 740

Url should be with a get parameter name. http://www.test.com/myphp.php?news=10

if(isset($_GET['news']) && is_numeric($_GET['news'])) {
     $newsUrl = 'http://www.test.com/news/' . (int)$_GET['news'] . '.php';
     header('location: ' . $newsUrl);
     // or 
     // header('location: /news/' . (int)$_GET['news']);
     die;
}

Upvotes: 1

Ava
Ava

Reputation: 2429

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).

Upvotes: 1

cmorrissey
cmorrissey

Reputation: 8583

You want to use a $_GET variable like so

http://www.test.com/myphp.php?p=10

and for your php you can do this.

header('Location: /news/' . $_GET['p'] . '.php');
die();

Upvotes: 1

Related Questions