homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite with web translations
chrisguk




msg:4598519
 9:39 pm on Aug 1, 2013 (gmt 0)

I currectly have translations embedded to my site where the user clicks a flag to choose the preferred language.


Presently the code looks like this:

Example 1
<a href="<? echo $_SERVER[PHP_SELF] . "?lang=en"; ?>"><img src="images/en_flag.gif" border=0></a>

The issue I have is that the solution does not work now I have (clean urls) implemented using this code in .htaccess:

Example 2
RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?page=$1 [L]

The link in the "Example 1" feeds the language into the following PHP code:



<?php
if ($_SESSION[lang] == "") {
$_SESSION[lang] = "en";
$currLang = "en";
} else {
$currLang = $_GET[lang];
$_SESSION[lang] = $currLang;
}
switch($currLang) {
case "en":
define("CHARSET","ISO-8859-1");
define("LANGCODE", "en");
break;
case "de":
define("CHARSET","ISO-8859-1");
define("LANGCODE", "de");
break;
default:
define("CHARSET","ISO-8859-1");
define("LANGCODE", "en");
break;
}
header("Content-Type: text/html;charset=".CHARSET);
header("Content-Language: ".LANGCODE);
?>



How can I get around this issue instead of the url looking like this:

http://www.example.com/index.php?lang=de

Any help will be great

[edited by: phranque at 12:56 am (utc) on Aug 2, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

 

JD_Toims




msg:4598526
 9:47 pm on Aug 1, 2013 (gmt 0)

How about changing the URL for "additional languages"?
<a href="/en/<? echo $_SERVER[PHP_SELF]; ?>">

Then grabbing the lang. from the beginning if it's present and rewriting?
RewriteRule ^([a-z]{2})?(([^/]+/)*[^/.]+)$ index.php?page=$2&lang=$1 [L]

Didn't test but it should be close.

chrisguk




msg:4598535
 9:57 pm on Aug 1, 2013 (gmt 0)

Hi there,

Thank you for your quick reply.

I tried your solution but this doesnt work for some reason.

Taking into account this was my original rewrite rule
RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?page=$1 [L]

It just takes me to a 404 not found

JD_Toims




msg:4598537
 10:04 pm on Aug 1, 2013 (gmt 0)

I forgot a / after the a-z to ensure it must be exactly 2 letters followed by / at the beginning and not any 2 letters. I also don't know if you're php is set up to accept lang=val as a variable, but that's something you'll have to look into and adjust as necessary.

RewriteRule ^([a-z]{2}/)?(([^/]+/)*[^/.]+)$ index.php?page=$2&lang=$1 [L]

See what happens when you don't test? lol

g1smd




msg:4598538
 10:08 pm on Aug 1, 2013 (gmt 0)

Your original code "Example 2" was missing the [QSA] flag. That flag re-appends the original query string on the end of the new query string that was set within the internal rewite.

However, while that's a quick way of getting it working, you should look at JD's solution in more detail. The example code in JD's post is simply missing a slash directly after the {2} in the RegEx pattern. I'm not sure about making the language "folder" optional. This makes the pattern ambiguous and is a Duplicate Content problem in the making (if e.g. /en/ and "no folder" return identical content).

Whatever you do, you MUST alter the PHP script so that it also returns a 404 HEADER and error message for ALL URLs requested with a non-valid language code.

You might find that putting your language data into an array and querying that is faster than using multiple CASE statements. The array is also easier to maintain, and can be moved elsewhere in the code, perhaps into a site configuration file.

Must type faster. Four minutes would have done it.

JD_Toims




msg:4598547
 10:25 pm on Aug 1, 2013 (gmt 0)

If the folder type URL is implemented you MUST alter the PHP script so that it also returns a 404 HEADER and error message for ALL URLs requested with a non-valid language code.

Exactly, good point!

I'm not sure about making the language "folder" optional. This makes the pattern ambiguous and is a Duplicate Content problem in the making (if e.g. /en/ and "no folder" return identical content).

That's another good point, but I was thinking optional so there was no redirect of the current URLs to a language specific directory. I think personally I would leave the current URLs + info in place and only add the lang. specific directories for alternate languages, then go with your point of 404ing any non-valid lang. code, including the current language the pages are published in.

So, the URL structure I think I would use is:
Assuming the current lang. is English

http://www.example.com/path-to/the-file [main lang - en]
http://www.example.com/fr/path-to/the-file [alternate lang]
http://www.example.com/es/path-to/the-file [alternate lang]
http://www.example.com/en/path-to/the-file [404 invalid lang code]

"Must type faster. Four minutes would have done it."
LOL You were on it quick though.

chrisguk




msg:4598562
 11:03 pm on Aug 1, 2013 (gmt 0)

Im real sorry about this.

I think I am kinda getting what you mean. I believe the issue is the way that my URL is formed in the link below:

<a href="/de/<? echo $_SERVER[PHP_SELF]; ?>"> <img src="images/de_flag.gif" border=0>

If you hover over that on the web page it, it looks like this:

http://www.example.com/en//index.php

Please forgive me for probably going around in circles

[edited by: phranque at 12:57 am (utc) on Aug 2, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

JD_Toims




msg:4598578
 11:27 pm on Aug 1, 2013 (gmt 0)

<a href="/de<? echo $_SERVER['PHP_SELF']; ?>"> <img src="images/de_flag.gif" border=0>

PHP_SELF, much like $_SERVER['REQUEST_URI'], begins with a leading / so the one after the lang code needed to be deleted.

(I didn't even think about it when I initially posted, because I rarely use PHP_SELF.)

chrisguk




msg:4598583
 11:41 pm on Aug 1, 2013 (gmt 0)

Ahh I see.

I will give you an example. When I click on the "de" flag for example. It takes me to this URL:

http://test.example.com/en/index.php

instead of:

The name of the page which is:

http://test.example.com/en/home

or

http://test.example.com/en/about

or whatever page you are on. Do know what I am trying to say.

Because the pages are included in the index.php like this:

// STANDARD PAGE HEADER AND FOOTER INCLUDES FOR DYNAMIC CONTENT

include(ROOT_DIR."/include/header.php");

if(file_exists(ROOT_DIR."/content/" . $page.".php"))
{

include(ROOT_DIR."/content/".$page.".php");

} else {

include(ROOT_DIR."/content/404.php");
}
include(ROOT_DIR."/include/footer.php");

[edited by: phranque at 12:58 am (utc) on Aug 2, 2013]
[edit reason] unlinked & exemplified url [/edit]

JD_Toims




msg:4598585
 11:46 pm on Aug 1, 2013 (gmt 0)

Try this instead:

<a href="/de<? echo $_SERVER['REQUEST_URI']; ?>"> <img src="images/de_flag.gif" border=0>

Again, I don't use PHP_SELF much, and part of the reason is it doesn't always do what you'd expect... If I remember correctly it actually references the currently executing file, not the requested location by the browser, but REQUEST_URI's value will be what the user sees in the address bar.

chrisguk




msg:4598591
 11:57 pm on Aug 1, 2013 (gmt 0)

I want to thank you for what you have advised so far. That doesnt work either.

What I will do is take a proper look at it tomorrow because its late here.

I may come back if I have troubles

JD_Toims




msg:4598603
 12:54 am on Aug 2, 2013 (gmt 0)

I want to thank you for what you have advised so far. That doesnt work either.

It's not a problem. If you need more help, you have to be more specific than "doesn't work", because it's tough to fix issues you can't see and don't have details about.

g1smd




msg:4598668
 6:00 am on Aug 2, 2013 (gmt 0)

Somewhere you need to define a variable that holds the page name, so that it can be used in links and elsewhere.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved