I am trying to translate these pages from English to French:
/about.php
/contact.php
/paintings.php?id={$id}&title={$title}
I created the following link:
<?php if($language !== 1): ?>English<?php else: ?>French<?php endif; ?>
With this code behind it:
$url = $_SERVER["REQUEST_URI"];
if (isset($_GET['lang'])) {
$pattern1 = '&lang=' . $_GET['lang'];
$pattern2 = '?lang=' . $_GET['lang'];
$patternTotal = array($pattern1, $pattern2);
$url = str_replace($patternTotal, '', $url);
$language = 1;
}
else if (strpos($_SERVER["REQUEST_URI"], '?')) {
$url .= '&lang=en';
$language = 0;
}
else {
$url .= '?lang=en';
$language = 0;
}
What I want for it to do is to return 1 to $language if the lang parameter is set to any value. Then I will use an if statement to fetch and display the content accordingly.
However I realized that this url /paintings.php?id={$id}&title={$title} looks bad and I used mod_rewrite to get it to this version /{$id}/{$title} with this RewriteRule ^([^/]*)/([^/]*)$ /photo.php?id=$1&title=$2 [L]. And things broke. While the $url works on all the other pages on the paintings one it doesn't.
I have a feeling that I got it all wrong. How can I make it work? Also, is there a more efficient way in doing this?
Related
Ok my website incorporates different languages, I did:
$lang = isset($_GET['lang']) ? $_GET['lang'] : "";
if (!empty($lang)) {
$curr_lang = $_SESSION['curr_lang'] = $lang;
} else if (isset($_SESSION['curr_lang'])) {
$curr_lang = $_SESSION['curr_lang'];
} else {
$curr_lang = "he";
}
if (!empty($pb20_14users->language == he)) {
echo "&lang=he";
}
if (file_exists("languages/" . $curr_lang . ".php")) {
include "languages/" . $curr_lang . ".php";
} else {
include "languages/he.php";
}
// Returns language key
function lang_key($key)
{
global $arrLang;
$output = "";
if (isset($arrLang[$key])) {
$output = $arrLang[$key];
} else {
$output = str_replace("_", " ", $key);
}
return $output;
}
The text has changed and everything is perfect, but I want to change my URL
&lang=he
But don't go through all the existing pages manually, they were built like this:
<div class="links">
Home<br />
Register<br />
Statistics<br />
Forgot password?
</div>
How do I make all addresses end & lang = he without going through one by one, if it matters I made a table in SQL called language and there is the word he, maybe it will help the thinkers, thank you very much
I recommend you go a (basic) nice url system, your urls will become /en/?page=foo which isnt the biggest rewrite in the url.
RewriteRule ^([a-z]{2})/(.*) /$2&lang=$1 [L]
I havent tested that line, but it should be close. We take everything before the slash (en) and add that to the rest (?page=foo) which results in ?page=foo&lang=en for your script.
Now you can do this and use CURRENT_LANG everywhere in your PHP for the language.
$lang = in_array($_GET['lang'], ['en', 'nl']) ? $_GET['lang'] : 'en';
define('CURRENT_LANG', lang);
Or try to use the <base /> tag in html.
I have the following files. The objective of this is to redirect to the correct news. For example:
localhost/tostadotv/esto-es-una-noticia-28.html
If I intentionally modify the url, for example:
localhost/tostadotv/esto-es-una-noticia-modificada-incorrecta-28.html
I should redirect myself to the correct news:
localhost/tostadotv/esto-es-una-noticia-28.html
However, it redirects me to this:
http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html
Where this error? Could you please help me thanks. Excuse my english I'm from Argentina I do not speak English
.htaccess
RewriteEngine On
RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]
noticia.php
<?php require_once("lib/connection.php"); ?>
<?php require_once("lib/functions.php"); ?>
<?php
fix_category_product_url();
?>
functions.php
function fix_category_product_url() {
$proper_url = get_proper_category_product_url(1);
if ( SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function get_proper_category_product_url($id) {
$product_id = $_GET['id_not'];
$query = sprintf('SELECT titulo FROM noticias WHERE id_not = "%d"', mysqli_real_escape_string($GLOBALS['DB'], $product_id));
$restit = mysqli_query($GLOBALS['DB'], $query);
$noticia = mysqli_fetch_array($restit);
$proper_url = make_category_product_url($noticia['titulo'], $product_id, $id);
return $proper_url;
}
define('SITE_DOMAIN', 'localhost');
function _prepare_url_text($string) {
$NOT_acceptable_characters_regex = '#[^-a-zA-Z0-9_ ]#';
$string = iconv('UTF-8','ASCII//TRANSLIT',$string);
$string = preg_replace($NOT_acceptable_characters_regex, '', $string);
$string = trim($string);
$string = preg_replace('#[-_ ]+#', '-', $string);
return $string;
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if ($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
As said in the comments, the final solution for the asker was to add http:// to the defined SITE_DOMAIN constant.
Before
define('SITE_DOMAIN', 'localhost');
After
define('SITE_DOMAIN', 'http://localhost');
But there's more to it than just that. Let's focus on the following two functions:
function fix_category_product_url(){
$proper_url = get_proper_category_product_url(1);
if(SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
The idea here is that $proper_url actually ends up getting a value from make_category_product_url() because its result is returned by get_proper_category_product_url(). It makes sense because make_category_product_url() has more parameters and uses the other to get their values.
What's funny about this is that the else block of the second function doesn't always return a path, but rather a URL. The problem here is that such URL is given without a defined protocol, but starts with the domain name instead. This value is therefore mistaken as a path.
Now take a look at the first function: it ultimately redirects the user using header('Location: '.$proper_url);. As we discussed earlier, $proper_url is not always a path, so the protocol should be added somewhere in the code whenever a URL takes place instead of a path. That's where the actual solution comes in: adding http:// where SITE_DOMAIN is defined is one way to do this, because this constant is only used when a URL takes place. There are many other ways to do this, but this one is completely valid.
Guys please help me with this
I want to replace this URL http://www.mywebsite.com/index?type=traveler with http://www.mywebsite.com/index/traveler
http://www.mywebsite.com/index?type=traveler is a hyperlink on some other page
I have tried many things but haven't succeeded till now.
You'll just need to add a few lines to your .htaccess file:
RewriteEngine On
RewriteRule index/(.*) index?type=$1 [L]
This will use "traveler" as your type parameter. It will catch any other value and send to the querystring as the type value as well.
Get the url and compare with this condition then include your page and exit.
if (strstr($url, 'index/')) {
$urlarr = explode('product/', $url);
$url = $urlarr[1];
$sing_ext = strpos($url, "'");
if ($sing_ext != false) {
$ur_ar = explode("'", $url);
$url = $ur_ar[0];
}
$sub_cats_url = $_REQUEST['tag'] = $urlarr[1];
include("product.php");
exit; }
I have a simple multi language website. The langauge of the displayed page is controlled by the use of a session variable, but I want users to be able to copy the url and send it to other people and end up on the same language page -- that is I want the "lang" url parameter to be present in the url always.
I could of course edit all links on the page and add it to them, but isn't there an easier way to do this? Is there an alternative solution?
Maybe you can try this:
<?php
//get full url
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//check if get lang exists.
if(isset($_GET['lang'])){
if($_GET['lang'] == "en"){
//then do nothing.
} else{
//get all parameters.
$query_arr = $_GET;
//chang lang parameter.
$query_arr["lang"] = "en";
$query = http_build_query($query_arr);
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
//make first part of url.
$first_url = 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];
//redirect to correct url.
header("location: " . $first_url . "?" . $query);
}
}else{
//redirect to correct url.
header("location: " . $url . "&lang=en");
}
?>
Hope this is wat you meant.
You can use something like:
<?php
session_start();
if(isset($_SESSION['lang'])){
$sessionLang = $_SESSION['lang'];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$rUri = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(!(isset($_GET['lang']))){
if (strpos($rUri, '?')) { // returns false if '?' isn't there
$newUrl = "$rUri&$sessionLang";
header("Location: $newUrl");
} else {
$newUrl = "$rUri?$sessionLang";
header("Location: $newUrl");
}
}
}
We make sure $_SESSION['lang'] isset.
Get the current url protocol and uri
Check if $_GET['lang'] isn't already set
Check if the url already contains parameters (strpos($_SERVER[REQUEST_URI], '?')), is so,
append &lang=, otherwise append ?lang= to it.
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?