PHP - Change every url in html depending on domain - php

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}";

Related

Grab URL using PHP

I have a script that grabs the current page url & stores it in a variable:
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
I then check this URL for a phrase to check what page the user is on.
if (strpos($page,'celtic') !== false) { echo 'this is the celtic page'; }
However, I have found a bug in this.
If the domain contains the phrase celtic, it won't work.
How can I test just the 3rd part of the URL so it would return true if the domain was www.celticfootball.com/teams/scotland/celtic/.
You can try do smth like this:
$params = $_SERVER['REQUEST_URI'];
switch(true) {
case (bool) strpos($params, 'celtic'):
echo 'celtic';
break;
case (bool) strpos($params, 'another'):
echo 'another';
break;
default:
//ignore
break;
}

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

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']

Safest way to parse url for PHP CMS?

As an exercise I am rolling my own CMS to better learn php. Currently I have my .htaccess file set to redirect all URLs to the index.php page.
// .htaccess file
RewriteEngine on
RewriteRule !\.(js|gif|jpg|png|css)$ index.php
Once I am at the index.php page, I will need to determine what content the user should see based off the requested URL. For example, if the user visited mysite.com/about then I would be including the file contents of about.php.
My question is: what is the safest way to parse this URL for the file value? I want to avoid the user writing malicious URLs which, among other things, could access private php files I don't want shown.
Your htaccess has really nothing to do with security. I suggest you use switch case like this:
$site = "home";
switch(strtolower($_SERVER['REQUEST_URI'])){
case "about":
$site = "about";
break;
case "contact":
$site = "contact";
break;
}
include "{$site}.php";
or maybe this:
$site = "home";
$page = strtolower($_SERVER['REQUEST_URI']);
switch($page){
case "about":
case "contact":
$site = $page;
break;
}
include "{$site}.php";
Or without having to make list of allowed pages
$path = "pages/";
$page = trim(strtolower($_SERVER['REQUEST_URI']),"/");
$slashPos = strpos($page, "/");
if($slashPos!==false){
$page = substr($page, 0, $slashPos);
}
$page = preg_replace("/[^a-z0-9-]/", "", $page);
if(file_exists("{$path}{$page}.php")){
include "{$path}{$page}.php";
}else{
//include "{$path}home.php";
}

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?

Categories