Apache Server configuration support - language negotiation

This is Interesting: Free IT Magazines  
Home > Archive > Apache Server configuration support > August 2006 > language negotiation





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author language negotiation
shimmyshack

2006-08-31, 1:43 pm

Hiya,

I have a simple question, how can I (using rewrites or some other
config) serve requests for non-existent files, say

filename.html

which have a content-negotiated filename on the system, say

filename.en.html
filename.fr.html ...

I wish all html coders editing webpages to be able to reference the
file simply by using its html sort form

filename.html

but to still allow the browser to negotiate the resulting file served,
say

filename.de.html, if they are german speakers

There are files on the system which do not have translations, so the
rewrite rule (if needed) would have to passthrough files matching

filename.html

where they DO exists, but will have to rewrite to
filename.{negotiated_lang}.html where they dont

I would appreciate all the help I can get, its had be baffled for a
couple an hour now.



PS. Apache can do this index.html -> index.html.en
BUT you have to use its default filenaming of

filename.html.{lang}

so why dont I just use the default Apache Language Negotiation Setup??
....

People who have to edit html files ending with multiple language code
get cross with the apache default, which forces someone managing a
website with 6 language translations for each file to set up 6 file
extension associations on their system, not everyone has the knowledge
or permission to do that. Plus the code editor/ide/syntax highlighter
of their choice might not be able to highlight html files if they dont
end with .html or .htm

Davide Bianchi

2006-08-31, 1:43 pm

On 2006-08-31, shimmyshack <matt.farey@gmail.com> wrote:
> I have a simple question, how can I (using rewrites or some other
> config) serve requests for non-existent files, say
>
> filename.html
>
> which have a content-negotiated filename on the system, say
>
> filename.en.html
> filename.fr.html ...


See http://httpd.apache.org/docs/2.2/co...egotiation.html

In short, the default configuration of Apache is already enabled
to use content-negotiation, but usually it uses
filename.html.language instead of filename.language.html, to do the
latter you have to map the files in a different way as explained in the
doc:

<from the doc>
An example map file is shown below. This file would be named foo.var,
as it describes a resource named foo.

URI: foo.en.html
Content-type: text/html
Content-language: en

URI: foo.fr.de.html
Content-type: text/html;charset=iso-8859-2
Content-language: fr, de

</from the doc>

Davide

--
....A Microsoft spokesperson said, "while fighting software piracy is good
for our business, highway robbery is our business."
-- From a Slashdot.org post
shimmyshack

2006-08-31, 1:43 pm

thanks for your response,

I need a transparent solution which reflected the fact that the editors
of the webpages would
a) reference all the files by filename.html - allowing apache to do the
negotiate
b) mean that adding a new translation in the form filename.{lang}.html
would immediately work
c) I cannot use the apache default Multiview setup as it leads to
annoyed users as it doesnt reflect the real world of file assocaitions
and editors.


Using Multiviews I have had much success, here are the 4 lines that do
transparent language negotiation from the domains vhost config

<Directory "vhosts/www.domain.com/public/">
Options +Indexes +MultiViews
DirectoryIndex index
</Directory>

What remains is to redirect users who try for
filename.html
(which doesnt exist but DOES have a translation or two, in the form
filename.en.html or filename.fr.html, and redirect them to)
filename
which would cause apache to transparently negoatiate them to the
correct resourse

I just cant figure out the details but I know enough to steer clear of
type-maps


Type-maps are a great solution if you enjoy hard-work - that kind of
setup is why CMS's have gained ground over simply knowing how your
webserver works.

I want a transparent, single-point-of-config solution whereas type maps
require at least the following
renaming all the links in every webapge to point to the var file
educating web content editors about how they now dont use standard
links to the files they are creating on the filesystem
creating multiple var files, and editing, proofing them
each new translation requires an addition to a var file.
the filesystem becomes awash with config files, and as mentioned the
structure of the filesystem becomes detatched from the content served
and html code written.
the files that make up the website are not transportable to a system
which has a different config without alot of file editing, whereas a
config file is easily changed

Davide Bianchi

2006-08-31, 1:43 pm

On 2006-08-31, shimmyshack <matt.farey@gmail.com> wrote:
> a) reference all the files by filename.html - allowing apache to do the
> negotiate


The only thing I can think of is to use some kind of scripting to check
for existence/inexistence of the related language-specific page and
eventually going to a default one if not present.

If you find something better, let us know.

Davide

--
Remember, you're dealing with developers. If they knew what they were
doing, they wouldn't be doing it.
-- Mike A
shimmyshack

2006-08-31, 1:43 pm

I like your quote on developers! very true

here is the stanza which I have found works:


<Directory "vhosts/domain.com/public/">
ReWriteEngine On
ReWriteBase /
ReWriteCond %{REQUEST_FILENAME} !-f
#WARN polish code is po on filesystem
ReWriteCond %{REQUEST_FILENAME}
!\.(en|fr|es|de|it|nl|no|po|pt|ru|sv|zh-CN|zh-TW)\.(htm|html)$
ReWriteRule ^(.*)\.(html|htm)$ $1 [L]
Options +Indexes +MultiViews
DirectoryIndex index
</Directory>

Note I am only matching the ends of filename strings here. (there is no
^ at the start of the reg exps)

This first tests to see if it a file ending with .html ot .html that
DOESNT exist (using -f, could have used -s for exists and size>0) this
matches filename.html and other files that have now been renamed
filename.{lang}.html

ReWriteCond %{REQUEST_FILENAME} !-f

Now we make sure that the file is ALSO (AND is default with rewrites)
NOT one that has a language code before the end .html or .htm. This
ensures that an internal request for a filename.{lang}.html doesnt get
matched, or we would get into an infinite loop

ReWriteCond %{REQUEST_FILENAME}
!\.(en|fr|es|de|it|nl|no|po|pt|ru|sv|zh-CN|zh-TW)\.(htm|html)$

Now we grab the first part of the filename before the .htm or .html.
Now this is the good bit, we rewrite filename.html to filename
this forces apache to negotiate for filename.{lang}.html according to
the MultiView process.
It is the last rule, [L]
ReWriteRule ^(.*)\.(html|htm)$ $1 [L]

Now we just add MultiViews to this directory

Options +Indexes +MultiViews
DirectoryIndex index



This has been tested on
images.(jpg|gif...)
filenames.htm(l) which do and dont exist,
filenames.en.htm(l) which do and dont exist

examples
request --> rewrite
filename -> /filename.{ang}.html
filename.{lang}.html -> passthrough
images.extension -> passthrough
and so on for sub directories...


If a file is requested directly such as filename.{lang}.htm which
doesnt exist on the filesystem, a 404 will be generated and handled by
error docs as per normal.

It is verified to work efficiently in all cases for apache 2.2.2 with
rewrite logging on 9.



Hope that helps someone.





> Remember, you're dealing with developers. If they knew what they were
> doing, they wouldn't be doing it.
> -- Mike A


Davide Bianchi

2006-08-31, 1:43 pm

On 2006-08-31, shimmyshack <matt.farey@gmail.com> wrote:
> I like your quote on developers!


ain't mine, is a quote from MikeA on alt.sysadmin.recovery

> here is the stanza which I have found works:


Well done.
Davide

--
Hey, you're right. I don't want to call a destructor on my objects, I
want to call a *destroyer*. Gozer has come for your memory, little
PersistentNode! -- Joel Gluth
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com