[mod_rewrite] passing variables 'silently'

Hey all
I have a simple CMS system that uses page paths to serve the correct data from a database. The problem now is that it is going to be multilingual and so I have had to rework the database tables to use the record ID rather than a simple path.

So my question is, how can I rewrite the url to maintain the integrity of my url’s but still pass an ID as well.

current url…


translates to


current mod_rewrite

RewriteRule ^([a-z]+)/?([a-zA-Z0-9_\\/]+)/?$ index.php?language=$1&page=$2&%{QUERY_STRING} [L]

New url:


needs to match the original url of


and not be


So basically the id variable is passed but not in the url…

Hope that made sense!

How should Apache know that “us/main_news_page” corresponds to the number 2 and not the number 4?

You also don’t need to append the existing query string yourself, use the QSA flag (query string append) and it’s combined for you.

Hi Mike!

Hmmm, looking at your regex, I’m a bit confused!

RewriteRule ^([a-z]+)/[COLOR="Red"][SIZE="4"]?[/SIZE][/COLOR]([a-zA-Z0-9_\\/]+)[COLOR="Red"][SIZE="4"]/?[/SIZE][/COLOR]$ index.php?language=$1&page=$2&%{QUERY_STRING} [L]

WHY is that first / optional? If the entire language part is optional, enclose the / in the atom and make the whole thing optional! WHY would you even consider allowing a trailing /?

THEN, [a-z] … will you be matching zz (or zzzzzzzzzz) for a language? Naw, I didn’t think so. As you add languages, you need to LIST the language options you will support in that first atom, i.e., ^((en|de|fr|es)/)? … to OPTIONALLY specify English, German, French or Spanish - with the language pair and / inside the optional atom’s definition.

([a-zA-Z0-9_\/]+)/? is okay except that the / need not be escaped by the blue \. For my “Wilderness-Wally” client, I’ve also allowed him to use [-a-zA-Z0-9_&'!] adding the hyphen, ampersand, single quote and exclamation to his titles (just because I’m a nice guy :jester2: ).

I’ve lost the rest of your question in the quoted parts but you should get the idea from the above. Teaching your CMS to embed the language and use it to specify the language for the specified title will be the difficult part (but I’m guessing you’ve already figured that part out!).

Oh, as Dan said, if you’re changing id numbers of the post, you’re going about it all wrong! First, you don’t use id numbers here (they’re not derivable from your title - or whatever field you’re searching) and second, you don’t need id’s - just check http://wilderness-wally.com’s pages if you don’t believe me (the stories are ALL requested by the (unique) title field in the record).