简体   繁体   中英

How to Change subfolder in url using php

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.

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!

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\/(.*)\/(.*)/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM