RegEx for RedirectMatch

Posted on 2006-05-13
Last Modified: 2008-01-16
Hello Experts,

I think this will be pretty easy for someone good with Regular Expressions. Unfortunately I am not such a person.

I am trying to use a RedirectMatch statement in my vhost.conf file to redirect certain calls to Apache.

All requests for:*

Should get redirected to:*

But requests that start:*
Should not be redirected or else we will create an infinite loop.

So for example, if the original request is for:
It should be redirected to

But if the original request is for:
It should not be redirected.

I was trying things like:

RedirectMatch ^/mydomain(.*)$1
RedirectMatch ^[^/mydomain](.*)$1

I am trying to get the regex to say "if the path does not start with "/mydomain", then copy the path and redirect to "".

I hope I am explaining this ok!  Thanks in advance for any help.


Question by:2djohn
    LVL 27

    Accepted Solution

    Are you using apache 2.x?

    If yes, you could use a negative lookahead

    RedirectMatch 301 ^/(?!mydomain/)(.*)$1

    Author Comment

    Hi Caterham,

    Fantastic- that seems to work!  So that I understand it:

    ^ means "match at the beginning of the path"
    /(?!mydomain/) means "match things that do not have 'mydomain/' after '/'
    (.*) is everything else, and that gets put into the $1

    LVL 27

    Expert Comment

    Yes, that's correct.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
    It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now