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.
-
Thank you very very mouch fro your help, i have fix that, but now I can not output the video titile and images, video:title & video:thumbnail_loc,Leo– Leo2019年01月24日 15:45:13 +00:00Commented Jan 24, 2019 at 15:45
-
<video:title>Video Title </video:title> <video:description> des </video:description> </video:thumbnail_loc> image </video:thumbnail_loc><video:duration>302</video:duration>Leo– Leo2019年01月24日 15:48:22 +00:00Commented Jan 24, 2019 at 15:48
-
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="sitemaps.org/schemas/sitemap/0.9" xmlns:video="google.com/schemas/sitemap-video/1.1"> <url> <loc>video post url/</loc> <video:video> <video:player_loc allow_embed="yes">yembed url/</video:player_loc> <video:title>TITLE</video:title> <video:description>DESCREPTIN</video:description> <video:thumbnail_loc>IMAGE.jpg</video:thumbnail_loc> <video:duration>384</video:duration> </video:video> </url>Leo– Leo2019年01月24日 15:52:24 +00:00Commented Jan 24, 2019 at 15:52
6 Answers 6
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
-
on html, how i add the url now? <a class="x" href="/<?php echo $url;?>">EN</a> ?Leo– Leo2019年01月29日 08:43:13 +00:00Commented Jan 29, 2019 at 8:43
-
Assuming the url is absolute i.e. starts with
https
or/
you need to<a class="x" href="<?php echo generateurl($url, 'en'); ?>">EN</a>
Salman Arshad– Salman Arshad2019年01月29日 08:45:44 +00:00Commented Jan 29, 2019 at 8:45
First I'd recommend that for English you keep the /en/
in the URL, it will be easier to manage.
Then to extract the language code and replace by an other value you can use
You can use preg_replace (REGEX)
$url = "https://mywebsite.com/en/foo";
$codes = [ 'it', 'fr', ...];
$urls = [];
foreach($codes as $code){
$urls[$code] = preg_replace("(https://mywebsite.com/)[a-z]{2}(.*)", "1ドル". $code. "2ドル", $url);
}
-
Thanks for your help, but I have more then 20 Languages on my website, I find it difficult to do that what I want, beaucose im not that good for script, just I like to do somthing, but thanks againLeo– Leo2019年01月14日 16:34:23 +00:00Commented Jan 14, 2019 at 16:34
-
If you have several languages, you just need to wrap one of the two options (the 2nd is recommended) in a
foreach
loopChewie– Chewie2019年01月14日 16:39:32 +00:00Commented Jan 14, 2019 at 16:39 -
i need som php code to do this function example, if after domain url mywebsite.com is /it/ or /es/ or /fr/ remove it.Leo– Leo2019年01月14日 16:49:31 +00:00Commented Jan 14, 2019 at 16:49
-
I updated the answer to show you an example of how you can implement it with a foreach loopChewie– Chewie2019年01月14日 16:56:28 +00:00Commented Jan 14, 2019 at 16:56
-
thanks again for your help, but in this case does not help me.Leo– Leo2019年01月14日 17:08:23 +00:00Commented Jan 14, 2019 at 17:08
Replacement of URL is bad practice. You should make two parts for work with URL:
- Parsing of language and URI without language
- A building of any URI with language
For example, parsing of language might be looked like:
function extractLanguage($uri)
{
if (preg_match('/^\/[a-z]{2}\//', $uri, $matches)) {
$language = trim($matches[0], '/');
} else {
$language = ''; // or default language
}
return $language;
}
extractLanguage('/search/?q=YouTube'); // will return empty string
extractLanguage('/al/search/?qURIuTube'); // will return 'al'
Parsing of URI without language might be looked like
function extractUri($uri)
{
$uri= preg_replace('/^\/[a-z]{2}\//', '', $uri);
if ($uri[0] !== '/') {
$uri = '/' . $uri;
}
return $uri;
}
extractUri('/search/?q=YouTube'); // will return '/search/?q=YouTube'
extractUri('/al/search/?q=YouTube'); // will return '/search/?q=YouTube'
If you will have separated language and separated URI you can build target URL, for example with help the following function
function buildUri($path, $params = [], $language = '')
{
$query = '';
if (!empty($params)) {
$query = '?' . http_build_query($params);
}
if (!empty($language)) {
$language = '/' . $language ;
}
return $language . $path . $query;
}
buildUri('/news/search', array('q' => 'YouTube')) // will return '/news/search/?q=YouTube'
buildUri('/news/search', array('q' => 'YouTube'), 'it') // will return 'it/news/search/?q=YouTube'
I've created a function that does this for you. Here is how it works:
- It removes the chosen language and everything after that from the url and puts that in
$startUrl
. - Takes the original url and removes the first part of it including the language, and puts that into
$endUrl
. - Adds the provided language to the middle and puts all three parts together, naming them
$newUrl
and returns that.
NOTE: This function requires you to specify language in every url, even for english!
NOTE 2: You might need to change the
$key
-value in both removing-parts of the function if you change the structure of your base url from https://mywebsite.com/ to, say, https://mywebsite.com/public/. I've added comments in the code about where they ́re located.
function createUrl($url, $language){
/*
* FIX THE BEGINNING OF THE URL
*/
// Explode the url into smaller parts and put into an array
foreach((explode('/', $url)) as $key => $value){
$expArray[$key] = $value;
};
// Remove the last part of the URL (including chosen language)
foreach($expArray as $key => $value){
if($key > 0){ /*<--This is one of the values you might need to be changed if your base url structure changes*/
unset($expArray[$key]);
}
}
// Implode the array back to a string
foreach($expArray as $key => $value){
$startUrl = implode('/', $expArray);
};
/*
* FIX THE END OF THE URL
*/
// Explode the url into smaller parts and put into an array
foreach((explode('/', $url)) as $key => $value){
$expArray[$key] = $value;
};
// Remove the first part of the URL (including chosen language)
foreach($expArray as $key => $value){
if($key < 2){ /*<--This is the other value you might need to be changed if your base url structure changes*/
unset($expArray[$key]);
}
}
// Implode the array back to a string
foreach($expArray as $key => $value){
$endUrl = implode('/', $expArray);
};
/*
* Put it all together
*/
if(isset($endUrl)){
$newUrl = $startUrl . $language . $endUrl;
return $newUrl;
}else{
$newUrl = $startUrl . $language;
return $newUrl;
}
};
To use it in your example you write it like this:
<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/EN/');?>"></a>
<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/AL/');?></a>
<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/IT/');?></a>
-
thanks for your help, but is not working, when i change from mywebsite.com/al to example mywebsite.com/it that is goin on this link mywebsite.com/al/itLeo– Leo2019年01月27日 18:23:59 +00:00Commented Jan 27, 2019 at 18:23
-
That is probably since your using
mywebsite.com/al
and nothttps://mywebsite.com/al
. Try adjusting the$key
-values accordingly.wenzzzel– wenzzzel2019年01月27日 19:34:01 +00:00Commented Jan 27, 2019 at 19:34 -
Allright, I've tried this out in a real apache environment instead of this php-fiddle tool I used before and it seems that my browser (chrome) cuts away the
https://
part. I've updated the code in the original answer to work with this scenario. Please try it out again. P.S I've allso added an if-statement at the bottom of the code to prevent an error when working with short urls.wenzzzel– wenzzzel2019年01月28日 18:48:53 +00:00Commented Jan 28, 2019 at 18:48 -
My solution! 'not perfect' beaucose if the user is searching on https://example.com/es/news/search/?q=news
when i change the language, example from es to en, the url change like this https://example.com/search/?q=news
if(isset($_GET["q"])) {
$qurl = $_GET["q"];
$surl = "/search/?q=";
}else{
$qurl = "";
$surl = "";
}
I suggest to use .htaccess configuration in Apache web server, in which you can handle all type of your request easily with hiding parameters, and you can easily redirect to your other urls.
The below urls will help you.
http://www.htaccess-guide.com/ - official Documentation
https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file- - htaccess example