1

I need to turn a query string like this:

http://localhost/view.php?id=12345

Into this:

http://localhost/12345

But, I also plan on having a string on the end of the URL for search engines to benifit from, like so:

http://localhost/12345/NameOfTheSkin

Kind of like how Wowhead do their URL's:

http://www.wowhead.com/item=49623/shadowmourne

Notice how you can change the string "shadowmourne" into anything and it'll still point to the item id 49623.

Basically, I need the string on the end to be ignored.

Any help is appreciated, cheers. :)

Josh
  • 1,361
  • 5
  • 22
  • 33

1 Answers1

5
RewriteRule ^/(\d+)/?.*$ /view.php?id=$1 [L]

This will rewrite http://host/1234/some-text and http://host/1234 into http://host/view.php?id=1234.

Detailed explanation:

^   -- matches at the start of the string
(   -- start match group
\d+ -- match one or more digits
)   -- end match group
/?  -- match 0 or 1 slash
.*  -- match any character 0 or more times
$   -- matches at the end of the string

Visit regular-expressions.info for complete guide of regexp.

Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
  • Why does that work? Could you include more details/explanation? – Joe May 16 '11 at 17:59
  • @Joe The first part of the rewrite rule `^/(\d+)/?.*$` is a regular expression that extracts digits `(\d+)` within the query string pattern `/xxx/?.*`. Where the `/?` tries to match a slash and `.*` matches everting to the end of the line `$`. This is then inserted to the request for view.php as `$1` meaning the part matched by the first parenthesis (the digits). – Captain Giraffe May 16 '11 at 18:07