After reading many articles here, I did not find a solution, so I need help to do this... My URLs are those Example:
Home Page
https://mywebsite.com/
https://mywebsite.com/al/
https://mywebsite.com/it/
https://mywebsite.com/videos/
https://mywebsite.com/al/videos/
https://mywebsite.com/it/videos/
https://mywebsite.com/news/
https://mywebsite.com/al/news/
https://mywebsite.com/it/news/
Query
https://mywebsite.com/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/news/search/?q=YouTube
https://mywebsite.com/al/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/al/news/search/?q=YouTube
https://mywebsite.com/it/search/?q=YouTube
https://mywebsite.com/it/videos/search/?q=YouTube
https://mywebsite.com/it/news/search/?q=YouTube
My php & html to change the language
<?php $Ava_Sulg = $_SERVER["REQUEST_URI"];?>
<a class="x" href="/<?php echo $Ava_Sulg;?>">EN</a>
<a class="x" href="/al<?php echo $Ava_Sulg;?>">AL</a>
<a class="x" href="/it<?php echo $Ava_Sulg;?>">IT</a>
so I'm allowing users to change their language, that what I want to do, is when they change the language the url can be one of the above, example if they change the language from AL to IT
and url is https://mywebsite.com/al/videos/search/?q=YouTube
with PHP
I want to get this https://mywebsite.com/it/videos/search/?q=YouTube
so I want to change from this url only (/al/ to /it/
) or exmaple from IT to EN
(/it/ to Nothing
) but that what I want to change is in the middle, and on home page is different, it is very difficult for me, How I can do this is this possible or no? I hope to find a solution here, if possible! Thank you very mouch.
Since you are only interested in manipulating the path component you can simply use parse_url
to extract the path component instead of regex:
function generateurl($url, $lc) {
$parts = parse_url($url);
$parts['path'] = preg_replace('@^/[a-z][a-z]/@', '/', $parts['path']);
if ($lc !== 'en') {
$parts['path'] = '/' . $lc . $parts['path'];
}
$url = '';
if (isset($parts['scheme'])) $url .= $parts['scheme'] . '://';
if (isset($parts['host'])) $url .= $parts['host'];
if (isset($parts['port'])) $url .= ':' . $parts['port'];
if (isset($parts['path'])) $url .= $parts['path'];
if (isset($parts['query'])) $url .= '?' . $parts['query'];
if (isset($parts['fragment'])) $url .= '#' . $parts['fragment'];
return $url;
}
The above function could be simplified if you have http_build_url
function available. Function input and output:
https://mywebsite.com/ + en = https://mywebsite.com/
https://mywebsite.com/ + al = https://mywebsite.com/al/
https://mywebsite.com/ + it = https://mywebsite.com/it/
https://mywebsite.com/al/ + en = https://mywebsite.com/
https://mywebsite.com/al/ + al = https://mywebsite.com/al/
https://mywebsite.com/al/ + it = https://mywebsite.com/it/
https://mywebsite.com/videos/ + en = https://mywebsite.com/videos/
https://mywebsite.com/videos/ + al = https://mywebsite.com/al/videos/
https://mywebsite.com/videos/ + it = https://mywebsite.com/it/videos/
https://mywebsite.com/al/videos/ + en = https://mywebsite.com/videos/
https://mywebsite.com/al/videos/ + al = https://mywebsite.com/al/videos/
https://mywebsite.com/al/videos/ + it = https://mywebsite.com/it/videos/
https://mywebsite.com/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube
/videos/search/?q=YouTube + en = /videos/search/?q=YouTube
/videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
/videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube
/al/videos/search/?q=YouTube + en = /videos/search/?q=YouTube
/al/videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
/al/videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With