I've spent some time searching for a simple solution for multilingual web site mod_rewrite rules, that would also support multiple directories. So, I've put together bits and pieces and here it is, a working example:
STEP 1: Create ".htaccess" file and put it in your web root directory with contents below.
RewriteEngine on RewriteRule ^en/(.*)/?$ index.php?action=$1&lang=en [NC,L] RewriteRule ^ru/(.*)/?$ index.php?action=$1&lang=ru [NC,L] RewriteRule ^de/(.*)/?$ index.php?action=$1&lang=de [NC,L] RewriteRule ^([^/].*)/$ index.php?action=$1 [NC,L] RewriteRule ^(.*.html)$ index.php?action=$1 [NC,L]
This example is most suitable for FILE BASED WEB SITE, where you can take [action] and append it to the PATH of content files.
Well, without any further introduction, here are small rules, that may be handy to know.
1) ([0-9]+) -> match if there any numbers in the string
GOOD: 123_word_with_numbers_456, 12345
BAD: word_with_no_numbers
2) ([a-zA-Z]+) - match if there any letters in the string
GOOD: 123_something_456
BAD: 12345
3) ([a-zA-Z0-9]+) - match any alpha-numeric string
GOOD: 123_something_456
BAD: @/~"!£$%^+-=*
4) ([0-9]{3}|[0-9]{2}|[0-9]{1}) - match any numeric string containing <=3 digits
Example:
RewriteRule ^search/([0-9]{3}|[0-9]{2}|[0-9]{1})/$ index.php?action=search&page=$1 [NC,L]
GOOD: example.com/search/123/
GOOD: example.com/search/12/
GOOD: example.com/search/1/
BAD: example.com/search/12345678/
5) ([^/][^/][^/]+) - match any string from 3+ characters and containing NO "/".
Example:
RewriteRule ^search/([^/][^/][^/]+)/$ index.php?action=search&q=$1 [NC,L]
GOOD: example.com/search/something 123/
BAD: example.com/search/ss/
6) ([^0-9^/][^0-9^/][^0-9^/]+) - match any string from 3+ characters and containing NO digits and NO "/"
Example:
RewriteRule ^search/([^0-9^/][^0-9^/][^0-9^/]+)/$ index.php?action=search&q=$1 [NC,L]
GOOD: example.com/search/something/
BAD: example.com/search/something 123/
BAD: example.com/search/ss/
Comments