views:

17

answers:

1

What is the rewrite rule for following URL

mysite.com/001234-some-keywords.html

I want to capture the 6 digit reference number something like ([0-9]{6})
my indexed file is

templates/default/index.php?ref=001234

One day I do a crashcourse regex.. I promise.

A: 

Probably

RewriteRule ^([0-9]{6}).*$ templates/default/index.php?ref=$1
  • ([0-9]{6}) was already correct, it captures the six digits
  • .* matches any character afterwards.
  • $1 gets replaced by the contents of the first capture group ()

If you want to restrict the URL to match only if there are exactly six digits, then it should look like this (note the dash -):

RewriteRule ^([0-9]{6})-.*$ templates/default/index.php?ref=$1

or matching the URL only if it is ending in .html:

RewriteRule ^([0-9]{6})-.*?\.html$ templates/default/index.php?ref=$1

I suggest to read the mod_rewrite documentation.
Also regular-expressions.info is great for learning regular expressions.

Felix Kling
That works, thanks. When I do mysite.com/001234-some-ke it also rewrites though.. is there a way to prevents this?
FFish
@FFish: If you tell me exactly what should be matched and what not... only URLs with `.html` at the end?
Felix Kling
I don't know. It's actually not that important if they try cutting of some keywords. Thanks Felix for the help and links!
FFish
OK I just tried with the .html option. This is exactly what I wanted. Thanks again.
FFish
@FFish: I assume the keywords are just for readability right? The actual identification is done by number, isn't it? If so, you should not care about whether people could change the keywords. The ID is the important thing. You can try it her on SO. As long as the ID is correct, the name behind can be garbage: http://stackoverflow.com/questions/2935245/sdbisdgawgwrg
Felix Kling