Reputation: 315
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.
Upvotes: 4
Views: 1285
Reputation: 1
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
Upvotes: 0
Reputation: 682
I've created a function that does this for you. Here is how it works:
$startUrl
.$endUrl
.$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>
Upvotes: 2
Reputation: 272086
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
Upvotes: 6
Reputation: 6456
Replacement of URL is bad practice. You should make two parts for work with URL:
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'
Upvotes: 2
Reputation: 315
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 = "";
}
Upvotes: 1
Reputation: 118
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);
}
Upvotes: 4