0

Please i need help to change requested subfolder URL to other one using php not htaccess file.

From stackoverflow.com/folder1/ask

To stackoverflow.com/folder2/ask

is that possible?

update:

I'm working on IP redirect based on geoip mod and bot detected function.

with 3 Magento website (single domain) and two store view with each website.

switch (true)
{ case (bot_detected($bot) === FALSE && $country == "us"):
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'us';

break;

case (bot_detected($bot) === FALSE && $country == "uk"):
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'uk';
break;

  default:
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
}

Its working fine but if US visitor click on URL from UK store redirect not applied. So what i need enforce the URL to redirected to US and same with any store conditions.

MagEGY
  • 111
  • 3
  • Some more context would need to be provided here for someone to be able to help. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Without content the best I can do is guess that a 301 redirect could could be created at the page in `folder1` which uses regex to find `/folder1/` and replace it with `/folder2/`. This answer can help walk you through doing this in php: http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – William Patton Sep 08 '16 at 15:18
  • Thank you @WilliamPatton I updated the question above. no i dont need the 301 redirect its geoip mod and bot detected function to redirect visitors to right store. – MagEGY Sep 10 '16 at 09:51

2 Answers2

0

You need to use the pathinfo function. Something like this works:

<?php

$url = 'http://stackoverflow.com/folder1/ask';

$pathinfo = pathinfo($url);

//$pathinfo['dirname'] contains the full prefix of the URL
//here, http://stackoverflow.com/folder1
//So simply replace folder1 by folder2 in that string
$newPrefix = str_replace('folder1', 'folder2', $pathinfo['dirname']);

//Use that new prefix and append the same basename ('ask')
$newUrl = $newPrefix . '/' . $pathinfo['basename'];

echo $newUrl;

Check it out on 3v4l.org!

SolarBear
  • 4,534
  • 4
  • 37
  • 53
  • Thank you SolarBear for your comment. i tried something like that before and the result was ERR_TOO_MANY_REDIRECTS. i just added header("Location: ".$newUrl); any idea? – MagEGY Sep 10 '16 at 10:06
  • Web servers will only allow a certain number of redirections : make sure you're not redirecting to the same page, otherwise you'll end up with an infinite loop of redirections (page A redirects to page A that in turn redirects to page A etc.) – SolarBear Sep 13 '16 at 19:27
0

A more general approach would be using RegEx, if you know the structure of your path.

So for example, you can use the following RegEx:

/stackoverflow\.com\/(.*)\/(.*)/
Victor
  • 13,914
  • 19
  • 78
  • 147