Mod_rewrite challenge

software development

#1

… 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 …

http://www.mysite.com/6-customer-stories/

to

http://www.mysite.com/page.php?menu=6

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.

mysite.com and mysite.fr serve us the same content (mysite.fr is an alias of mysite.com).

If the user does not arrive via one of my friendly URLs, but rather at site root, I need www.mysite.com (index.php) to serve up the home page in English, i.e. http://www.mysite.com/page.php?menu=1 and for www.mysite.fr (index.php) to serve up the home page is French, i.e. http://www.mysite.fr/page.php?menu=22.

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, http://www.mysite.fr/27-temoignages-clients/

So the .htaccess file must detect that, although the %{HTTP_HOST} is www.mysite.com, the ID of the content being requested falls within the French range, so the RewriteRule should redirect to http://www.mysite.fr/27-temoignages-clients/

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.

Thansk.


#2

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.


#3

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.

Thanks.


#4

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

1 through 21:

22 through 41:


#5

How about

(2[2-9]|[3-4][0-9])

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

!(2[2-9]|[3-4][0-9])

#6

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.


#7

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


#8

Thanks everyone. I’m all set now.