Switching beetween languages - mod_rewrite

Hello

The problem:

for instance
http://www.domain.com/en/about/index.html 
This page has a german  button with this link http://www.domain.com/en/about/index.html?de

By clinking on it, I would like to get the page http://www.domain.com/de/about/index.html

If http://www.domain.com/en/about/index.html does not exist, http://www.domain.com/en/about/index.html?de reload the same page

Any ideas ?
LVL 8
us111Asked:
Who is Participating?
 
caterham_wwwConnect With a Mentor Commented:
may be have a try on this:

RewriteEngine on
# /en/about/index.html?de --> /de/about/index.html only,
# if /de/about/index.html exsists (and other way)
RewriteCond %{QUERY_STRING} ^(de|en)
# other language version page exsists
RewriteCond %{DOCUMENT_ROOT}/%1/$2 -f
# -> rewrite
RewriteRule ^/(de|en)/?(.*) /%1/$2? [R=301,L]

# If http://www.domain.com/en/about/index.html does not exist, but
# if http://www.domain.com/de/about/index.html exists, load that page/redirect to it:
# en page does not exsist
RewriteCond %{REQUEST_FILENAME}  !-f
# german page does exsist
RewriteCond %{DOCUMENT_ROOT}/de/$1 -f
# redirect to german page
RewriteRule ^/en/?(.*)$ /de/$1 [R=301,L]


Make sure, that %{DOCUMENT_ROOT} dies not have a triling slash, otherwise remove the slash from the condition like %{DOCUMENT_ROOT}de/$1
0
 
stefanaichholzerCommented:
us111,

If the page doesn't exist you could use the server error pages, to redirect to the page you want, just place a simple script to redirect to the wanted page. I guess the page-not-found error page is 550, not really sure...

Let me know how it goes...

;)
0
 
us111Author Commented:
error 404
yes but it couldn't be a solution. We don't want to generate error pages.

no more ideas with mod_rewrite ?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
manav_mathurCommented:
>If http://www.domain.com/en/about/index.html does not exist, http://www.domain.com/en/about/index.html?de reload the same page

if http://www.domain.com/en/about/index.html did not exist, how would http://www.domain.com/en/about/index.html?de be possible in the first place??

probably you are referring to if the de version does not exist.

RewriteEngine On
RewriteCond /actual/path/to/de/index.html ! -s
RewriteRule ^/en/(.*)$ /de/$1 [R,L]

0
 
us111Author Commented:
I will try tomorrow. Time to leave.

However, RewriteCond /actual/path/to/de/index.html ! -s
ok but how could I build this path dynamically. My question was an example.

it could be http://www.domain.com/en/services/index.html?de to redirect to http://www.domain.com/de/services/index.html 

I would like to test if the page exists because there's a delay between the english and german translation.




0
 
manav_mathurCommented:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}  ! -s
RewriteRule ^/en/(.*)$ /de/$1 [R,L]
0
 
us111Author Commented:
for instance, the HTML structure:

en
---about
-----index.html  (<a href="index.html?de">de</a>)
---services
-----index.html (<a href="index.html?de">de</a>)
---index.html (<a href="index.html?de">de</a>)
de
---about
-----index.html (<a href="index.html?en">en</a>)
---index.html (<a href="index.html?en">en</a>)

if I click on de in the page en > about > index.html  I would like to redirect to de > about > index.html.

I think I need one more condition : if ?de or if ?en
0
 
caterham_wwwCommented:
where did you place the rules? This is for .htaccess:

RewriteEngine On
# note: there must not be a space between ! and -f
RewriteCond %{REQUEST_FILENAME}  !-f
# make sure, you're sendung a 301 redirect, not a 302
RewriteRule ^en/?(.*)$ /de/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(de|en)
RewriteRule ^(de|en)/?(.*) /%1/$2? [R=301,L]


this for httpd.conf (per-server context)

RewriteEngine On
# note: there must not be a space between ! and -f
RewriteCond %{REQUEST_FILENAME}  !-f
# make sure, you're sendung a 301 redirect, not a 302
RewriteRule ^/en/?(.*)$ /de/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(de|en)
RewriteRule ^/(de|en)/?(.*) /%1/$2? [R=301,L]
0
 
us111Author Commented:
into the httpd.conf

if I enter directly into /en/index.html, I am automatically redirect to /de/index.html
0
 
caterham_wwwCommented:
make sure, REQUEST_FILENAME is correct. If everything is configured right, it contains for a request of example.com/test/12.html the full physical filepath like /var/www/user1/test/12.html, where /var/www/user1 is your document root
0
 
us111Author Commented:
all path are corrects.

this works:
RewriteCond %{QUERY_STRING} ^(de|en)
RewriteRule ^/(de|en)/?(.*) /%1/$2? [R=301,L]


but if http://www.domain.com/en/services/index.html?de and http://www.domain.com/de/services/index.html does not exist http://www.domain.com/en/services/index.html?de should do nothing or redirect to the same page

I think these conditions are not at the good place
RewriteCond %{REQUEST_FILENAME}  !-f
# make sure, you're sendung a 301 redirect, not a 302
RewriteRule ^/en/?(.*)$ /de/$1 [R=301,L]
??

Time to leave (week-end).

I will try this next monday.



0
 
stefanaichholzerCommented:
us111,

You don't have to generate error pages, make a new 404 error page, and place a JavaScript document.location redirection.

That way if the page is not found it will directly move to the new page, without producing any error at all, the user won't notice it at all...

;)
0
 
stefanaichholzerCommented:
Well, I guess the points should be splitted, in any case, we all gave suggestions on how to fix the issue. This is a recommendation only and I'm sure you guys, admins, will know what to do...

;)
0
 
us111Author Commented:
sorry for the delay, but not really enought free time
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.