URL重定向到GET变量

时间:2017-04-16 17:50:41

标签: 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规则?

2 个答案:

答案 0 :(得分:0)

在包含以下内容的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

答案 1 :(得分:0)

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();
相关问题