0

I have a problem rewriting strings in the .htaccess file.

For letters I successfully used

    RewriteRule page/([0-9]+)$ page.php?num=$1

I tried on the same model to use:

    RewriteRule page/([a-z]+)/$ page.php?word=$word

were word can be a string from any length. I am expecting for example page/hello to be rewritten in page.php?word=hello. But this is not functional.

Thank you for your help.

Machavity
  • 28,730
  • 25
  • 78
  • 91
michltm
  • 1,157
  • 2
  • 9
  • 27
  • 2
    `$1` refers to group number 1: the regex enclosed within `()`. So in both cases you should use `$1`. Say for example you got `user/([0-9]+)/([a-z]+)/?$` then you can use `page.php?id=$1&name=$2`. – HamZa Jan 11 '16 at 13:12
  • 2
    @HamZa you should set this out as an answer. – Martin Jan 11 '16 at 13:16
  • @Martin Arguably, it could be a duplicate of http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained – HamZa Jan 11 '16 at 13:21

1 Answers1

2

$1 refers to group number 1: the regex enclosed within (). So in both cases you should use $1.

To clear up the confusion, lets give another example. Say you got

user/([0-9]+)/([a-z]+)/?$

then you can use

page.php?id=$1&name=$2

$1 refers to ([0-9]+) and $2 refers to ([a-z]+)

HamZa
  • 13,530
  • 11
  • 51
  • 70