Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I list about 9,000 records on my little site. At the moment I'm showing them with a dynamic page, like


But I'd like to start using meaningful URLs like this one on Amazon


where the title string on the root level gets ignored and requests are redirected to the records.php page, which accepts the ID as usual.

Does anybody know how I could achieve that with mod_rewrite? I'm wondering how I'd deal with requests to my other root-level pages, like http://domain.com/contact.php, that I don't want to redirect to the records page.

share|improve this question

migrated from stackoverflow.com Jun 25 '11 at 10:30

This question came from our site for professional and enthusiast programmers.

mod_rewrite supports regular expressions, so you'll probably end up using a regular expression that matches /[title-string]/[id] (such as ^/[\w-]*/(\d+)$), and redirect that ignoring the title string. Unfortunately I don't have enough mod_rewrite experience to tell you the syntax off the top of my head! – jtbandes Jun 25 '11 at 7:36

Try this in your root's .htaccess:

RewriteEngine on
RewriteRule ^[^/]+/(\d+)$ records.php?id=$1 [QSA]

http://example.com/foo-bar/42 will be rewritten to http://example.com/records.php?id=42

It wont affect URLs like contact.php, since it does not match ^[^/]+/(\d+)$

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.