In a PHP website I want to set a redirect if there are variables in the URL inside a specific folder (.com/promo/).
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?
Create .htaccess
in the apache DirectoryRoot containing the following:
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
RedirectToFolder();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.