0

How can i rewrite this url:

http://localhost/?=register

To look like this?:

http://localhost/index.php/Register
3
  • possible duplicate of Access GET variables with PHP + .htaccess Commented Oct 17, 2014 at 20:17
  • It looks like you just need to change the url the button links to. Commented Oct 17, 2014 at 20:18
  • index.php/Register/ redirection is not working, trust me, i've tried it already. Commented Oct 17, 2014 at 20:20

2 Answers 2

0

Your redirection code:

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

For accessing the /Register/ value, use:

$_SERVER['PATH_INFO']
Sign up to request clarification or add additional context in comments.

Comments

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", ""

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.