mod_rewrite and pretty urls

Posted by Peeter on Stack Overflow See other posts from Stack Overflow or by Peeter
Published on 2010-04-05T01:05:28Z Indexed on 2010/04/05 1:13 UTC
Read the original article Hit count: 403

What I'm trying to achieve:

1) http://localhost/en/script.php?param1=random is mapped to http://localhost/script.php?param1=random&language=English

  • This has to work always.

2) http://localhost/en/random/text/here will be mapped to http://localhost/categories.php?term=random/text/here

  • This has to work if random/text/here is 404

What I have at the moment:

RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]

RewriteRule ^en/(.*) $1?lang=English&%{QUERY_STRING} [L]
RewriteRule ^ee/(.*) $1?lang=Estonian&%{QUERY_STRING} [L]
RewriteRule ^ru/(.*) $1?lang=Russian&%{QUERY_STRING} [L]
RewriteRule ^fi/(.*) $1?lang=Finnish&%{QUERY_STRING} [L]

What I've thought:

substr(%{REQUEST_FILENAME},3) would fix my problem (as currently /ee/index.php is literally mapped to /ee/index.php instead of just /index.php)

Unfortunately I couldn't find a way to manipulate strings :/

© Stack Overflow or respective owner

Related posts about apache

Related posts about mod-rewrite