2014-10-17 97 views
0

我怎麼能改寫這個網址:重寫URL的從PHP重定向

http://localhost/?=register 

要這個樣子?:

http://localhost/index.php/Register 
+0

的可能重複[訪問GET變量與PHP +。 htaccess](http://stackoverflow.com/questions/3375680/access-get-variables-with-php-htaccess) – Machavity 2014-10-17 20:17:12

+0

它看起來像你只需要改變按鈕鏈接到的網址。 – jeroen 2014-10-17 20:18:07

+0

index.php /註冊/重定向不工作,相信我,我已經嘗試過了。 – Yildirim 2014-10-17 20:20:22

回答

0

您重定向代碼:

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

爲了訪問/寄存器/值,使用:

$_SERVER['PATH_INFO'] 
0

在使用header命令執行重定向之後,您必須記得編寫代碼來處理該URL。

/index.php/Register將嘗試加載內部「/index.php/」文件夾中的「註冊」的文件...... 404錯誤

所以,你需要使用Apache modrewrite重定向這些「虛擬文件夾「轉換爲可處理它的集中式腳本。

像這樣的的.htaccess:

RewriteEngine on 

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

然後,在你的index.php,你將把進來的URL來檢測該文件,做任何你想用它。

我通常使用catch-all(將所有內容重定向到/index.php),然後中斷URL並處理它,這樣我就可以擁有任意數量的虛擬文件夾/文件。下面是我自己的功能,以處理任何傳入的請求:

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

所以extractUri( 「/ index.php文件/註冊」)將返回:

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