[英]Rewriting URL's from a PHP Redirection
How can i rewrite this url: 我该如何重写此网址:
http://localhost/?=register
To look like this?: 看起来像这样?:
http://localhost/index.php/Register
Your redirection code: 您的重定向代码:
header('Location: /index.php/Register/',true,302);
exit;
For accessing the /Register/ value, use: 要访问/ Register /值,请使用:
$_SERVER['PATH_INFO']
After performing the redirection using the header command, you must remember to write your code to process that URL. 使用header命令执行重定向后,必须记住要编写代码来处理该URL。
/index.php/Register will try to load a "Register" file inside "/index.php/" folder ... 404 error /index.php/Register将尝试在“ /index.php/”文件夹中加载“注册”文件... 404错误
So you will need to use apache modrewrite to redirect these "virtual folders" to a centralized script that can handle it. 因此,您将需要使用apache modrewrite将这些“虚拟文件夹”重定向到可以处理该文件的集中式脚本。
Something like this on .htaccess: 在.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. 然后,在index.php中,您将使用传入的URL来检测该文件,然后对它进行任何处理。
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. 我通常使用包罗万象的东西(将所有内容重定向到/index.php),然后断开URL并对其进行处理,因此我可以拥有任意数量的虚拟文件夹/文件。 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: 因此extractUri(“ / index.php / Register”)将返回:
Array ([0] => "/", [1] => "index.php"), "Register", "Register", ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.