• Last Modified
  • 08.04.2007

Mod_Rewrite Examples

Multiple directory + Multilingual Support

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]

CASE EXAMPLES:

  • example.com/articles/mod-rewrite/ -> GET: [action]=>articles/mod-rewrite/
  • example.com/de/articles/mod-rewrite/ -> GET: [action]=>articles/mod-rewrite/ [lang]=>de
  • example.com/ru/articles/mod-rewrite.html-> GET: [action]=>articles/mod-rewrite.html [lang]=>ru

This example is most suitable for FILE BASED WEB SITE, where you can take [action] and append it to the PATH of content files.



Advanced Mod_Rewrite Techniques

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/	


  • Bookmark this page on Delicious
  • Bookmark this page on StumbleUpon
  • Digg this page on Digg
  • Submit this page on Reddit
  • Submit this page on Furl
  • Share this page on Facebook
  • Bookmark this page on Google
  • Bookmark this page on Yahoo
  • Search for links to this page on Technorati
  • Search for links to this page on IceRocket

Comments

2007-10-09
alfred
thanks alot it`s very useful but how to point: somesite.com/fixed/something >> somesite.com/?s=something some site.com/fixed/otherthing >> somesite.com/?s=otherthing wich means my links right now is like that: /?s=name i`d like to make them /fixes/name greetings

2007-12-01
sasha brinkova
Contrary to popular belief, the end of the year is one of the best times to look for, awesome descion


Leave a comment (E-mail address never displayed)





2006-2007 @ SkyByte.net. All Rights Reserved xhtml 1.0 css