简体   繁体   English

URL重定向到GET变量

[英]URL redirect to GET variables

In a PHP website I want to set a redirect if there are variables in the URL inside a specific folder (.com/promo/). 在一个PHP网站中,如果特定文件夹(.com / promo /)中URL中有变量,我想设置重定向。

When a user visits: 用户访问时:

www.example.com/promo/Marco-Aurelio

Redirect to: 重定向至:

www.example.com/promo/?user=Marco-Aurelio

What PHP code or htaccess rules would you use? 您将使用什么PHP代码或htaccess规则?

Create .htaccess in the apache DirectoryRoot containing the following: 在包含以下内容的apache DirectoryRoot中创建.htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/promo/(?![?])(.+)
RewriteRule ^ /promo/?user=%1 [L]

So that the url 这样的网址

http://www.example.com/promo/Marco-Aurelio

Will be redirected to 将被重定向到

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

So just call the function after the php opening tag 因此,只需在php开头标签之后调用该函数

RedirectToFolder();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM