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.

3
  • 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. Commented Jan 11, 2016 at 13:12
  • 2
    @HamZa you should set this out as an answer. Commented Jan 11, 2016 at 13:16
  • @Martin Arguably, it could be a duplicate of stackoverflow.com/questions/20563772/… Commented Jan 11, 2016 at 13:21

1 Answer 1

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]+)

Sign up to request clarification or add additional context in comments.

1 Comment

Ok great, thanks, I hadn't realized that $1 was refering to the group number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.