0

I'm looking for a way to change URLs if a condition is met. Something like:

URL: http://mysiteurl/

$url = 'http://mysiteurl/';

if ($userGroup == 'ABC') { 
    $url .= 'user/ABC/';
}

The new URL will be: http://mysiteurl/user/ABC/

But the page accessed will be the same.

4
  • Have you found a solution?
    – sv3n
    Commented Jul 20, 2017 at 9:18
  • I didn't implement your solution yet, this task is paused, but I created a alternative way without change magento URL structure using window.history.pushState(null, null 'mysiteurl/userABC/'); in javascript to create a fake URL. And redirecting at index.php with header('Location: mysiteurl/'); Commented Jul 20, 2017 at 16:49
  • I still intend to implement your solution and check if it best suits my question. If it's true I will give you the check mark ✔. Commented Jul 20, 2017 at 16:58
  • I agree, I will add my solution. Commented Jul 20, 2017 at 16:59

2 Answers 2

0

I'll give you a logic then you implement it in an observer.

$url = Mage::getModel('core/url_rewrite')->getRequestPath(); //www.domaine.com/index.php/
$CustomerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId(); //02 for exemple
echo Mage::getUrl($url, array('user' => $CustomerGroup));  
//www.domaine.com/index.php/user/02/
2
  • Ok, understood! But why I should do an observer for this? and what event should I observ? Commented Jul 12, 2017 at 12:10
  • an observer to do it for all pages, else you have to do it in each page and is not a best practice, the event i don't know try this one controller_front_init_routers but im not sure
    – PЯINCƎ
    Commented Jul 12, 2017 at 12:30
0

Maybe it's not the best solution, but to do a generic implementation without change magento URL structure I add this on index.php to make a redirect to http://mysiteurl/ if the user go to http://mysiteurl/user/ABC/

$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

$get = '';
$first = true;

// create get string
foreach ($_GET as $key => $value) {
    $get .= $first ? '?' : '&';
    $get .= $key . '=' . $value;
    $first = false;
}

// remove get from url
$url = str_replace($get, '', $url);

preg_match("/user.*/", $url, $pregMatchUser);

if($pregMatchUser) {
    $url = preg_replace("/user.*/", '', $url);

    // add get to url
    $url .= $get;

    header('Location: ' . $url);
    exit();
}

And then create a fake URL with JavaScript, using window.history.replaceState:

var url = window.location.toString();
var userGroupUrl = 'user/' + App.userGroup + '/';
var newUrl, index = url.indexOf("?");
if(index == -1) {
    newUrl = url[url.length - 1] == '/' ? url + userGroupUrl : url + '/' + userGroupUrl;
} else {
    newUrl = url.replace('?', userGroupUrl + '?');
}

window.history.replaceState({}, document.title, newUrl);

Of course, if you do this you will not be able to use /user/ in your URL for another thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.