Mod_rewrite challenge

software development


… for me, anyway.

I have a PHP/MySQL website that displays content in either English or French. PHP codes works correctly to populate the website with the correct content – when Apache receives a properly structured Request URI and query string. That’s where I’m stuck. I’m using mod_rewrite to make friendly URLs.

For example, I can successfully rewrite …


where 6 is the unique ID of a record in my webcontent database.

It becomes complicated for me when I try to account for the fact that records 1 to 21 are English content, and records 22 to 43 are French content. and serve us the same content ( is an alias of

If the user does not arrive via one of my friendly URLs, but rather at site root, I need (index.php) to serve up the home page in English, i.e. and for (index.php) to serve up the home page is French, i.e.

Also, if the user is navigating around the .com site and decides to switch to French, he clicks the FR language switch link to, say,

So the .htaccess file must detect that, although the %{HTTP_HOST} is, the ID of the content being requested falls within the French range, so the RewriteRule should redirect to

In summary, my rewrite conditions and rules need to:

I’m looking for a solution that is scalable because I’ll likely be adding other languages. And I’m really hoping to avoid a complete site recode and database restructuring to implement this language display and switching a different way.

Any help would be greatly appreciated.



You should be implementing this in your code, not htaccess. Your approach is not really scalable unless you maintain a very small site and are willing to update htaccess with every change / addition.


Yes, now that I think about it harder, I realise that I got a bit carried away thinking I could do this with mod_rewrite. I created the rewrite rules for friendly URLs naturally moved on to a related task, without realising that achieving my goals would be easier in PHP.

But since I don’t like to leave a puzzle unsolved (even if I’m not the one who solves it), how can regex recognise if a number is within the range 1-21 or the range 22-41. This seemed anything but straightforward.



I don’t recommend this, but just for the sake of answering the question:

1 through 21:

22 through 41:


How about


for 22-41 and everything not that is 1-21?



Your first regular expression would match 49. If that’s OK then cool. :slight_smile:

The second one wouldn’t work, since the regular expression was meant to be embedded inside a larger URL match. Inverting the whole thing would have the wrong effect.


Ah, good point. I’ve been using too much awk lately…


Thanks everyone. I’m all set now.