Search code examples
regexhttp-redirectregular-language

Regular expressions for 301 redirect


How can I redirect http://example.com/news.php?id=268&page=25 and all pages like http://example.com/news.php?id=X&page=Y to another page using regular expressions?

I tried using http://example.com/news.php?id=/d&page=/d.


Solution

  • Digits are matched with \d, not /d. To match at least 1 digit, you need to add the + quantifier. And to capture the numbers so you can copy them to the replacement URL, you need a capture group around it.

    You also need to escape certain characters that have special meaning in regular expressions: . and ?.

    http://example\.com/news\.php\?id=(\d+)&page=(\d+)