Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite check for string in URL

         

yeah

9:28 am on Jan 7, 2009 (gmt 0)

10+ Year Member



Hello,

I'm new to mod_rewrite and i've got a problem checking two conditions.
First I want to know what domain the visitor entered (.com, .co.uk, .de etc.).
After that I want to know if the URL contains the string "l:".
Some examples:
example.com/page/ --> redirects to example.com?page=page&l=EN
example.de/page/ --> redirects to example.com?page=page&l=DE
example.de/ --> redirects to example.com?page=&l:DE
example.de/page/l:EN --> redirects to example.de?page=page&l=EN

I created the following mod_rewrite:
RewriteCond %{HTTP_HOST} ^dory-php5$ [NC]
RewriteCond %{REQUEST_URL} !^.*l:.* [NC]
RewriteRule ^([a-zA-Z0-9_]*)$ ?page=$1&l=EN [L]

RewriteRule ^([a-zA-Z0-9_]+)/(.*)$ ?page=$1&l=$2 [L]

I hope this makes sense to anyone.

Thanks in advance!

Roel

jdMorgan

3:00 pm on Jan 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first RewriteCond is malformed, in that the HTTP_HOST server variable is only going to contain a hostname, not a URL-path. So, it's not clear what you intended with that, but it does not look correct.

A bigger problems is that a domain-country-code does not map directly to a language-code. I would actually suggest that you do not try to map the domain-country-code or the HTTP_ACCEPT_LANGUAGE header value to the language, because there is no guarantee that you won't get a request from, for example, a Thai businessman in a Paris hotel.

Also, people in many countries speak two or more different languages. Two examples are Canada-ca (en or fr) and Switzerland-ch (fr or de). Then there are the millions of people using computers in internet cafes, where the computer has never had its preferred-local-language set, and HTTP_ACCEPT_LANGUAGE probably defaults to English. Those cases will have to be handled in the script.

The bottom line is that it is best to let the user explicitly set the language.

A good approach is one default-language home page with a world map or a bunch of flags that the user can click to set a language cookie, and then serve content based on that cookie.

But with your current approach and based on some assumptions, I would suggest:


RewriteCond %{QUERY_STRING} !&?l=[^&]+
RewriteCond %{HTTP_HOST}>en ^([^.]\.)+com>(.+)\.?(:[0-9]+)?$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]\.)+([a-z]{2})\.?(:[0-9]+)?$ [NC]
RewriteRule ^([a-z0-9_]*)$ ?page=$1&l=%2 [NC,L]

Get that working, then duplicate the RewriteConds for your second rule.

Note the "trick" used to map .com to "en".

Note that the trailing "\.?(:[0-9]+)?$" in the last two RewriteCond patterns allows for trailing periods and port numbers on the requested hostname, and allows the patterns to be end-anchored. Similarly, the "&?" and "[^&]+" patterns in the first RewriteCond serve to "anchor" the "l=" name/value pair between two ampersands if either or both of those ampersands are present, and thereby prevent confusing "l=" with something like "tail=".

Jim

Caterham

3:45 pm on Jan 7, 2009 (gmt 0)

10+ Year Member



redirects to example.com?page=page&l=EN

And that resolves to index.php (with query_String page=page...)? Specify that file in your substitution instead of just the query_string to avoid a subrequest lookup looking for your DirectoryIndex file and an internal fast redirect (fixup hook of mod_dir, processing in apache 1.3 differs since mod_dir acts as "content" handler there). If you have the chance to do so, use it.