Search code examples
regexapache.htaccessurl-rewriting

Rewrite from a55 but not a555, a5555 etc


I currently have /a1234 (any number after letter "a") rewriting to page.php?var=1234, I have 1 particular page with number 55 I need to redirect elswehere.

rewriterule ^a55 /home/public_html/otherpage.php

While this simple line works, it also redirects /a555, /a5555 etc... to otherpage.php

  • How do I only have it redirect just 55 and not 555 etc. ?

Solution

  • rewriterule ^a55 /home/public_html/otherpage.php
    

    This matches any URL-path that simply starts a55. The ^ is a start-of-string anchor. You also need an end-of-string anchor ($) to match just that URL-path. For example:

    RewriteRule ^a55$ /home/public_html/otherpage.php [L]
    

    You should also include the L flag, to prevent further processing. And this rule must go before your more general rule.

    Reference: