在PHP網站中,如果特定文件夾(.com/promo /)中的URL中存在變量,我想設置重定向。將URL重定向到GET變量
當用戶訪問:
www.example.com/promo/Marco-Aurelio
重定向到:
www.example.com/promo/?user=Marco-Aurelio
你會用什麼PHP代碼或htaccess的規則?
在PHP網站中,如果特定文件夾(.com/promo /)中的URL中存在變量,我想設置重定向。將URL重定向到GET變量
當用戶訪問:
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
將被重定向到
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();
內部服務器錯誤後 – MaryOne
之初 – Hossam
我得到了同樣的錯誤加上'RewriteEngine敘述on'線。 – MaryOne