Jung
Jung

Reputation: 219

url rewriting an id with a string variable

trying to figure out how to rewrite this url clientside

blog.com/post/how-to-get-the-ladies

to point serverside to

blog.com/post.php?id=123

i know how to do this:

blog.com/post/123

RewriteRule ^([^/]+)/?$ post.php?id=$1 [NC,L]

but how do you replace the id string with the post title slug?

Upvotes: 1

Views: 2528

Answers (3)

hannasm
hannasm

Reputation: 2041

I solved a similar problem recently. I would suggest looking into the RewriteMap directive and using an 'External Rewriting Program'.

There are some big limitations with RewriteRule in terms of maintainability and robustness. If you haven't gotten there yet you may eventually. Only simple rewriting rules can be written safely.

With a rewriteMap you can create a php or perl script, take advantage of your existing code base, and perform all the rewriting rules from a localized place in your code which easily sits in version control.

I believe you need access to the httpd.conf (or vhost) configuration file though, RewriteMaps (or some related directive) cannot be put in .htaccess files.

Upvotes: 1

merkuro
merkuro

Reputation: 6177

The webserver itself doesn't make this distinction and cannot translate from your "unique text identifier" to the database id. Therefore a .htaccess rule alone evaluated by the webserver will not help you. But how is it done on all those web-applications? Normally this translation is done by Joomla/Wordpress itself and it only works as long the "how_to_get_the_ladies" text is known and unique throughout the system/database.

Upvotes: 2

Haim Evgi
Haim Evgi

Reputation: 125664

you can add rule that go to index file like :

RewriteRule ^(.*)$ index.php?url=$1

and in this file according to the title you can show the post that request

Upvotes: 2

Related Questions