URL Rewriting Multiple ID's

I’m trying to find some URL Rewriting code to make some of my URLS Prettier. Most of the code I’ve seen basically takes one type of url and tranforms that e.g

www.somesite.com?productid=12121&typeid=2323 to something like www.somesite/product/12121/type/2323

I have ID’s that relate to seperate articles

e.g www.somesite.com?ID=1 —> www.somesite.com/seo-for-beginners
e.g www.somesite.com?ID=2 —> www.somesite.com/creating-great-websites

Woud I have to add rewrites for all the ID’s in httaccess and is this a bad practice?

Thanks i advance


ARGH! I loathe those, too! HOWEVER, you must provide useful information within your links for mod_rewrite to redirect TO (you have it backward) a file which Apache can serve.

Years ago, as a Mentor (before jumping to Team Leader), I summarized “everything you REALLY need to know about mod_rewrite” in a tutorial which I provided to SitePoint and have maintained at http://dk.co.nz/seo. It provides information from enabling mod_rewrite on your own test server to developing good mod_rewrite code (NOT the (.*) nonsense which gets most newbies in trouble) to real world examples with discussion and usable code. Over the years, it has helped MANY SitePoint members and should give you a hint about using the title rather than id in your links (and using _'s rather than -'s to stand in for spaces). As a mature example of using article titles (with all the “special characters” which do not cause problems), have a look at http://wilderness-wally.com. Please note that, due to my work load, I’ve resigned as TL and am not here often but I do respond fairly quickly to PMs if you have any questions on the above.

YES, it would be VERY bad practice to string line after line after line in your mod_rewrite because the .htaccess file must be read for each and EVERY request (including .css, .js, .jpg, .yadda-yadda) files so, IMHO, that would be an abuse of the server. When you can use the title rather than ID (per Wilderness Wally’s website) for building links, your title-to-id redirection would be ridiculous at best. IF I had a client who insisted on your title-to-id conversion, I’d do that in a PHP handler file and have that redirect to your ID’d version of the link (preferably via an include rather than header(“location”) redirection). But, why …?



Hi.Thanks for the reply.I think you have it backwards,I’m not sure.I have current dynamic pages that use ids.I want to convert these ids into pretty titles instead of the id number it currently is.

This would mean cross referencing the id to a title.Ive found I can use a mapping text file or perl code to get the new URL title from a database bt I’m now not sure with all the work involved if it would give me much gain.

This kind of thing is typically done with PHP (or whatever your server language is) and your database, not with rewrite rules, because rewrite rules simply lack the information necessary to convert a pretty title to an ID.

The gist is, you rewrite all or some URLs to your script…


RewriteRule ^articles/ articles.php

Then your PHP checks the $_SERVER['REQUEST_URI'] variable and extracts relevant information, such as the pretty title (also called a slug).

// $_SERVER['REQUEST_URI'] is /articles/seo-for-beginners
if (preg_match('#^/articles/([^/]+)$#', $_SERVER['REQUEST_URI'], $matches)) {
    $slug = $matches[1];

Then you would look up the article in your database. The only difference is you’re finding it by slug rather than by ID.

SELECT * FROM articles WHERE slug = :slug

Of course this requires that you have a slug field (unique and indexed) in your database that you set when you create a record.


Au contraire, mon ami. It is for YOU to create the links in any format you wish; it is for Apache to serve a file; and, it is for mod_rewrite to convert YOUR format into a request Apache can understand and use to find the appropriate file.

To use a RewriteMap, you MUST have access to the server’s configuration file (or vhosts config file) which will NOT happen unless you’re on a VPS or dedicated server.

Did you even bother to look at http://wilderness-wally? That was all done using PHP to generate the links and mod_rewrite to convert the request for the file handler to select the correct record from the database. All that was needed was to use the title field rather than the id field (which does require unique titles), a two-way exchange of spaces for _'s and significant care in which special characters are allowed in the title (a concern with allowable characters in a URI - see http://www.ietf.org/rfc/rfc2396.txt).

Of course, it’s up to you.



Thanks again for all the replies.

I think this is just over my head at the moment and would take me a lot of time, testing and problems :smile: I do have access to the server, it’s my own VPS. I did look at wilderness wally’s url structure too, no idea how it’s doing that though :smile: If you have some example or tutorial on that please let me know, that’s exactly the kind of thing I am wanted to do.

Whether I am right or wrong after a bit of reading is, that static urls (or static looking urls) are slightly better for SEO so I’ve decided just to modify what I have, whether it helps I don’t know, maybe a last response from you guys could tell me if it’s really bad or not before I completely go down that road

I have articles on the site at these urls e.g :

I am now just going to add some extra details to the 1 paramater instead of the ID number

It’s more readable for the search and hopefully google will pick up on the words.


I’m kind of getting it now Jeff_Mott a little bit more. Thanks but still off.

I want a user to type in http://www.site.com/article/top-ten-seo-tips.html this html page does not exist anywhere
Then I want from that to look up the data somehow e.g top-ten-tips.html maps some how to ID=1 in the database

Like what http://wilderness-wally/ does

I want : http://www.site.com/article/top-ten-seo-tips.html to know it’s really articles.html and then look up the title to get the data from the database



Hmmm, I’ve tried to explain it here AND in the tutorial. In brief, the side column nav links are generated from the database and are the titles of the various articles (albeit, I’d added some code to group a series on a single subject, the Miscellaneous and Tonga Episodes groups - ignore those at this juncture). The titles are unique in the database, are restricted to characters allowed in URIs (see the link above - there’s a lot of techno-babble but the allowable characters are defined there) and spaces have been replaced with _'s (because -'s do occur in the language and the space replacing character must be unique). I use a SINGLE mod_rewrite statement to match the allowed URI string and redirect to the file which provides the articles (via a database query after “safing” the title string and swapping back to spaces). In other words, it’s DIRT SIMPLE. The crux of the matter is that ALL links have been provided (meaning that a default page - the Home Page, actually - is provided if the “title string” is not in the database).

I loathe displaying the id which conveys NO information and showing the file handler with a query string (TITLE, NOT ID) is silly (IMHO). To me, wilderness-wally.com/Welcome or wilderness-wally.com/World’s_Shortest_Books for URLs is warm, friendly and they each convey useful information.

If you need more help than that, please take it offline and PM me with your questions.



All Done.

Using 1 redirect line and adding the code

if (preg_match(‘#^/articles/([^/]+)$#’, $_SERVER[‘REQUEST_URI’], $matches)) {
$slug = $matches[1];

Thanks all for your help. I’ve just redirected anything with ^articles/ to my original code with the above code in it to query the database to get the article.

So I can now have nice pretty urls like site.com/articles/nice_article_name

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.