Search code examples
regexapache.htaccessmod-rewriteurl-rewriting

How to use parse special characters literally in Apache rewrite rule?


I have an Apache rewrite rule and I need to parse the ? character from the middle part (^page.php?...) literally - not take it as a special character. But I want other special characters to work normally.

What's the correct way to do this?


Solution

  • You can't use a query string that way. You need to use query string in a condition to check if it is there. Then use the captured backreference in the rule. Let me know how it works for you.

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^viewstory=([0-9]+)$
    RewriteRule ^news\.php$ index.php?page=news&viewStory=%1 [R=301,L]