tags:

views:

46

answers:

4

I've been asked to develop a mod_rewrite to change

http://www.example.com/health-and-fitness-tips/999/

into

http://www.example.com/health-and-fitness-tips/how-do-I-lose-10kg-in-12-weeks/

where 999 is the id and How do I lose 10kg in 12 weeks is the title.

Is this even remotely possible or is mod_rewrite only for changing something like

http://localhost/siteengine/products.php?id=123

into

http://localhost/products/123

???

+3  A: 

That's not practically do-able AFAIK ( dynamic-wise, without extra modules ). The first step you need to do is convert all the links to SEO ones, then you need to feed the titles as a parameter to a page.

Example:

<a href=/health-and-fitness-tips/how-do-I-lose/>

Condition: ^/health-and-fitness-tips/(.*)$/
Rewrite to: ^/page.php?title=$1

Some of this is pseudo code, but parse $_GET['title'] in page.php and query the database based on the permalink.

You'll also need to setup 301s for each permalinked URI so search engines don't get lost after you've done this.

meder
A: 

Mod_rewrite just applies regular expression to the URL so getting ID from Title or vai versus is not possible (as far as I know).

NawaMan
+1  A: 

You could do this, but it would be messy and laggy. You'd still need a script to perform the rewriting.

Check out the documentation for the RewriteMap directive, in particular the bit on the prg map type -- using an external program to provide URL mapping services.

Now, mind you, I think this is a bad, bad idea, but it would let you get this done.

Charles
+2  A: 

i'm guessing the answer meder gave is the one you're looking for but technically you can create a static map file to redirect a set of title strings to ids and it doesn't have to execute an external prg executable:

RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]

with the contents of the /tmp/title_to_id.txt file something like this:

how-do-I-lose-10kg-in-12-weeks  999
some-other-title                988
and-another                     983
jnichols959
Handy solution. This could be very useful if you cannot edit the code that made the poorly named pages.
Tom Leys
Meder's answer technically answered my question but didn't provide a workable result. I'm actually 80% through implementing your answer as I type this. Thank you!
ivannovak