jeff
jeff

Reputation: 13653

.htaccess conflicting rewrite rules

I'm trying to change a website to multi-language, so I have URL's like this:

www.company.com/en/about
www.company.com/fr/about

which should point to index.php?lang=en&what=about

so I defined the following rewrite rule (which works)

RewriteRule    ^en/(.*)$    ?lang=en&what=$1    [NC,L]
RewriteRule    ^fr/(.*)$    ?lang=fr&what=$2    [NC,L]

but I also need the homepage url as www.company.com/en (pointing to index.php?lang=en) which does not work for this rule.

The best solution would be something like

RewriteRule    ^(.*)/(.*)$    ?lang=$1&what=$2    [NC,L]

but it converts all the urls, like href='css.css' kind of references, so it messes up the whole page.

so how should I restrict the first GET variable to be two chars? or one of the defined languages?

Upvotes: 0

Views: 108

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

Try:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /?lang=$1&what=$2 [L]

The first grouping, ([a-z]{2}), captures the 2 letter language. The second optional grouping captures the "what". If there's nothing there, then "what" will be blank.

Upvotes: 1

Related Questions