0

How can i rewrite this url:

http://localhost/?=register

To look like this?:

http://localhost/index.php/Register
Yildirim
  • 41
  • 1
  • 1
  • 7

2 Answers2

0

Your redirection code:

header('Location: /index.php/Register/',true,302);
exit;

For accessing the /Register/ value, use:

$_SERVER['PATH_INFO']
cyprien
  • 65
  • 6
0

After performing the redirection using the header command, you must remember to write your code to process that URL.

/index.php/Register will try to load a "Register" file inside "/index.php/" folder ... 404 error

So you will need to use apache modrewrite to redirect these "virtual folders" to a centralized script that can handle it.

Something like this on .htaccess:

RewriteEngine on

RewriteRule ^index.php/(.*)$    index.php

Then, at your index.php, you will treat the incomming URL to detect that file, and do whatever you want with it.

I usually work with a catch-all (redirects everything to /index.php) and then break the URL and treat it, so I can have any number of virtual folder/files. Here is my own function to treat any incoming request:

function extractUri($uri="") { 
    if ($uri == "") $uri = isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != "" ? $_SERVER['REQUEST_URI'] : "";
    if ($uri != "") {
        # removes query from request
        if ($uri[0] != "/") $uri = "/".$uri; # ALWAYS START WITH /
        $uri = explode("?",$uri);
        $uri = str_replace("..",".",array_shift($uri)); # little exploit remover
        $uri = explode("/",str_replace("//","/",$uri));
    } else
        $uri = array("/");

    $context = array();
    foreach ($uri as $part)
        array_push($context,preg_replace("/(\.){2,}/","\.",$part)); # prevents ..

    $action = array_pop($context); # file (with extension)
    $original_action = $action; # preserve the original file with extension (I work w/o them)
    $ext = "";
    if ($action == "") $action = 'index'; # so if you are accessing folder/, then you probably want the index
    else if (strpos($action,".")!==false) { # remove extension
        $action = explode(".",$action);
        $ext = array_pop($action);
        $action = implode(".",$action);
        $action = removeSimbols($action,true,false); # makes sure it is a valid filename, this function removes any weird character
    }
    return array($context,$action,$original_action,$ext); # returns the folder structure (array), the page/action, the page/action with extension, and said extension (lots of repetition to speed up later)
}

so extractUri("/index.php/Register") would return:

Array ([0] => "/", [1] => "index.php"), "Register", "Register", ""