重写URL的从PHP重定向
问题描述:
答
您重定向代码:
header('Location: /index.php/Register/',true,302);
exit;
为了访问/寄存器/值,使用:
$_SERVER['PATH_INFO']
答
在使用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", ""
的可能重复[访问GET变量与PHP +。 htaccess](http://*.com/questions/3375680/access-get-variables-with-php-htaccess) – Machavity 2014-10-17 20:17:12
它看起来像你只需要改变按钮链接到的网址。 – jeroen 2014-10-17 20:18:07
index.php /注册/重定向不工作,相信我,我已经尝试过了。 – Yildirim 2014-10-17 20:20:22