Search code examples
rubyregexrack-rewrite

Regex in Rack-Rewrite redirecting more than it should


I have a redirect setup like so:

r302 %r{(/example/.*)}i, 'http://new-domain.com/$1'

For this URL http://example.com/example it works great redirecting to http://new-domain.com/example

I would like to adapt this regex (or expression) to not redirect if the URL has more than one level of directory, such as http://example.com/admin/example


Solution

  • Basically we simply anchor the start of the string with ^. which matches at the start of a string and is zero-width:

    r302 %r{^(/example/.*)}i, 'http://new-domain.com/$1'