[英]URL redirect to GET variables
在一个PHP网站中,如果特定文件夹(.com / promo /)中URL中有变量,我想设置重定向。
用户访问时:
www.example.com/promo/Marco-Aurelio
重定向至:
www.example.com/promo/?user=Marco-Aurelio
您将使用什么PHP代码或htaccess规则?
在包含以下内容的apache DirectoryRoot中创建.htaccess
:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/promo/(?![?])(.+)
RewriteRule ^ /promo/?user=%1 [L]
这样的网址
http://www.example.com/promo/Marco-Aurelio
将被重定向到
http://www.example.com/promo/?user=Marco-Aurelio
function RedirectToFolder(){
//lets get the uri
$uri = $_SERVER["REQUEST_URI"];
//remove slashes from beginning and ending
$uri = trim($uri,"/");
//lets check if the uri pattern matches the uri or not
//if it doesnt match dont continue
if(!preg_match("/promo\/(.+)/i,$uri)){
return false;
}
//lets now get the last part of the uri
$explodeUri = explode("/",$uri);
$folderName = end($explodeUri);
//lets get the new url
$redirectUrl = "http://www.example.com/promo/?user=$folderName";
Header('Location:'.$redirectUrl);
exit();
}//end function
因此,只需在php开头标签之后调用该函数
RedirectToFolder();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.