How to Change subfolder in url using php - 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\/(.*)\/(.*)/
Related
PHP - Change every url in html depending on domain
Using PHP I would like to be able to change every url in a specific HTML page depending on domain. This code allows me to change urls one by one but I'm sure there is a better way: $domain = $_SERVER['SERVER_NAME']; switch ($domain) { case "example2.com": $url1 = "http://".$domain."/secondsubfolder/thispage.html"; break; case "example3.com": $url1 = "http://".$domain."/thirdsubfolder/thispage.html"; break; default: $url1 = "http://example.com/firstsubfolder/thispage.html"; } HTML: first url Notice that I also need to change the first subfolder depending on domain, thats why I can't use relative urls. So my aim is to be able change every url in my default HTML code: hello bye should change if my website is accessed through example2.com <a href="example2.com/secondsubfolder/thispage.html" >hello</a> bye
Your question isn't very clear to me but I guess you need something like: <?php $domain = $_SERVER['SERVER_NAME']; $scriptPath = $_SERVER['SCRIPT_NAME']; switch ($domain) { case "example2.com": $domain = "example2.com"; $folder = "secondsubfolder"; break; case "example3.com": $domain = "example3.com"; $folder = "thirdsubfolder"; break; default: $domain = "example.com"; $folder = "thirdsubfolder"; } $scriptPath = preg_replace('%^/.*?/%', "/$folder/", $scriptPath); echo "http://{$domain}{$scriptPath}";
Get url from script usage
So i'm writing a script in PHP which generates a image so other people can use it too. But is it possible to get the url's on the pages the scripts are used ? For example. http://www.johnexample.com is using my image with this format <img src="http://www.myurl.com/image.php"> Now i wan't to receive the url of http://www.johnexample.com without GET variables if possible. It's basically a script that's suppose to track/note down all the websites that are using my image. At first i though it was possible with this: $url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; But that only get's the location of the script itself. Thanks
Oh, that was simpler than i though. Got it working like this now. The page with the tag only has to load once and it will save. Only using Session now because it's being tested local. Gonna switch it over to a database. Thanks guys <?php session_start(); $url = $_SERVER["HTTP_REFERER"]; if(!strpos($_SESSION["url"], $url)) { if($url != '') { $_SESSION["url"] = $_SESSION["url"] . "," . $url; } } $tracker = explode(",", $_SESSION["url"]); var_dump($tracker); ?>
how to get real address of urls in php switch statement
we have local PHP files A-Z. where redirection switch statement coded. Actually i want to request with id and get REAL address of redirection url. for example, domain1.com full path, domain2.com full path. please somebody let me know, how to get REAL Domain path of these urls? <?php $id = $_GET['id']; switch ( $id ) { case 01: header("Location: http://domain1.com"); break; case 02: header("Location: http://domain2.com"); break; default: header("Location: http://"); break; } ?> How i request by id? http://localhost/a.php?id=123 and its return Real Address of url like this: http://www.domain.com/content/title.html // etc
You need something like this? $path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Try $parts = parse_url('http://domain1.com'); echo $parts['host']; output will we domain.com and if you need path use $parts['path']
Adding a extra path after domain
If trying to add a extra path in URL after my domain in all urls $extra = "en"; $domain = "http://domain.com"; $current = currentURL(); $rest_path = str_replace($domain,'',$current); $result = $domain."/".$extra.$rest_path; // $result is "http://domain.com/en/mysub/mysub" After this so I redirect my site via using PHP redirect To get current url is doing like.. function currentURL() { $pageURL=(#$_SERVER["HTTPS"]=="on")?"https://":"http://"; if($_SERVER["SERVER_PORT"]!="80"){ $pageURL.=$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; }else{ $pageURL.=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } It's look like many steps , Any easier to do that ? Or review my bad coding pls. PS : Try to do without using .htaccess
I would use just: $extra = "en"; $domain = "http://domain.com"; $result = $domain."/".$extra.$_SERVER['REQUEST_URI']; since you are not using protocol or domain name.
$extra = 'en'; $domain = $_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI']; $new_url=str_replace('.com','.com/'.$extra,$domain); No?
check for subdomain in parse_url
I am trying to write a function to just get the users profile id or username from Facebook. They enter there url into a form then I'm trying to figure out if it's a Facebook profile page or other page. The problem is that if they enter an app page or other page that has a subdomain I would like to ignore that request. Right now I have: $author_url = http://facebook.com/profile?id=12345; if(preg_match("/facebook/i",$author_url)){ $parse_author_url = (parse_url($author_url)); $parse_author_url_q = $parse_author_url['query']; if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){ $fb_id = "/".$match[1];} else{ $fb_id = $parse_author_url['path']; } $grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square"; } echo $gav_url; This works if $author_url has "id=" then use that as the profile id if not then it must be a user name or page name so use that instead. I need to run one more check that if the url contains facebook but is a subdomain ignore it. I belive I can do that in the first preg_match preg_match("/facebook/i",$author_url) Thanks!
To ignore facebook subdomains you can ensure that $parse_author_url['host'] is facebook.com. If its anything else like login.facebook.com or apps.facebook.com you need not proceed. Alternatively you can also ensure that the URL begins with http://facebook.com as: if(preg_match("#(?:http://)?facebook#i",$author_url)){
This isn't a direct solution for what you were asking but the parts are here to do what you need to do. I found that a subdomain resulted in an issue with parse_url. Namely it returned an array with only $result['path'] and no 'host' or 'scheme'. My theory here is if there is no 'host' or 'scheme' results from parse_url and it has domain suffix ( .ext ) in the string, it is a subdomain. Here is the code: (the $src is a url I had to sort out the relative src from subdomains ): $srcA = parse_url( $src ); //..if no scheme or host test if subdomain. if( !$srcA['scheme'] && !$srcA['host'] ){ //..this string / array is set elsewhere but for this example I will put it here $tld = "AC,AD,AE,AERO,AF,AG,AI,AL,AM,AN,AO,AQ,AR,ARPA,AS,ASIA,AT,AU,AW,AX,AZ,BA,BB,BD,BE,BF,BG,BH,BI,BIZ,BJ,BM,BN,BO,BR,BS,BT,BV,BW,BY,BZ,CA,CAT,CC,CD,CF,CG,CH,CI,CK,CL,CM,CN,CO,COM,COOP,CR,CU,CV,CW,CX,CY,CZ,DE,DJ,DK,DM,DO,DZ,EC,EDU,EE,EG,ER,ES,ET,EU,FI,FJ,FK,FM,FO,FR,GA,GB,GD,GE,GF,GG,GH,GI,GL,GM,GN,GOV,GP,GQ,GR,GS,GT,GU,GW,GY,HK,HM,HN,HR,HT,HU,ID,IE,IL,IM,IN,INFO,INT,IO,IQ,IR,IS,IT,JE,JM,JO,JOBS,JP,KE,KG,KH,KI,KM,KN,KP,KR,KW,KY,KZ,LA,LB,LC,LI,LK,LR,LS,LT,LU,LV,LY,MA,MC,MD,ME,MG,MH,MIL,MK,ML,MM,MN,MO,MOBI,MP,MQ,MR,MS,MT,MU,MUSEUM,MV,MW,MX,MY,MZ,NA,NAME,NC,NE,NET,NF,NG,NI,NL,NO,NP,NR,NU,NZ,OM,ORG,PA,PE,PF,PG,PH,PK,PL,PM,PN,POST,PR,PRO,PS,PT,PW,PY,QA,RE,RO,RS,RU,RW,SA,SB,SC,SD,SE,SG,SH,SI,SJ,SK,SL,SM,SN,SO,SR,ST,SU,SV,SX,SY,SZ,TC,TD,TEL,TF,TG,TH,TJ,TK,TL,TM,TN,TO,TP,TR,TRAVEL,TT,TV,TW,TZ,UA,UG,UK,US,UY,UZ,VA,VC,VE,VG,VI,VN,VU,WF,WS,XXX,YE,YT,ZA,ZM,ZW"; $tldA = explode( ',' , strtolower( $tld ) ); $isSubdomain = false; foreach( $tldA as $tld ){ if( strstr( $src , '.'.$tld)!=false){ $isSubdomain = true; break; } } //..prefixing with the $host if it is not a subdomain. $src = $isSubdomain ? $src : $src = $host . '/' . $srcA['path']; } Could write a further confirmation by parsing the subdomain==true strings before the first '/' and testing against characters with a RegEx. Hope this helps some people out.