2017-04-16 136 views
0

在PHP網站中,如果特定文件夾(.com/promo /)中的URL中存在變量,我想設置重定向。將URL重定向到GET變量

當用戶訪問:

www.example.com/promo/Marco-Aurelio 

重定向到:

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

你會用什麼PHP代碼或htaccess的規則?

回答

0

在apache DirectoryRoot創建.htaccess包含以下內容:

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

這樣的URL

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

將被重定向到

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

+0

內部服務器錯誤後 – MaryOne

+0

之初 – Hossam

+0

我得到了同樣的錯誤加上'RewriteEngine敘述on'線。 – MaryOne

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