Php URL重定向以获取变量

Php URL重定向以获取变量,php,.htaccess,redirect,mod-rewrite,url-redirection,Php,.htaccess,Redirect,Mod Rewrite,Url Redirection,在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_UR

在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]
因此,url

http://www.example.com/promo/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
将被重定向到

http://www.example.com/promo/?user=Marco-奥雷里奥

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();

内部服务器错误开始时在行上添加重写引擎是否安装了apache模块mod_rewrite?意识到它会导致无限重定向,因此出现错误500。这个问题现在已经解决了。检查上面。