• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1790
  • Last Modified:

RegEx for RedirectMatch

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:
http://www.mydomain.com/*

Should get redirected to:
http://www.mydomain.com/mydomain/*

But requests that start:
http://www.mydomain.com/mydomain/*
Should not be redirected or else we will create an infinite loop.

So for example, if the original request is for:
http://www.mydomain.com/index.htm
It should be redirected to http://www.mydomain.com/mydomain/index.htm

But if the original request is for:
http://www.mydomain.com/mydomain/index.htm
It should not be redirected.

I was trying things like:

RedirectMatch ^/mydomain(.*) http://www.mydomain.com/mydomain/$1
RedirectMatch ^[^/mydomain](.*) http://www.mydomain.com/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 "http://www.mydomain.com/mydomain/path".

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

John

0
2djohn
Asked:
2djohn
  • 2
1 Solution
 
caterham_wwwCommented:
Are you using apache 2.x?

If yes, you could use a negative lookahead

RedirectMatch 301 ^/(?!mydomain/)(.*) http://www.mydomain.com/mydomain/$1
0
 
2djohnAuthor Commented:
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

Correct?
0
 
caterham_wwwCommented:
Yes, that's correct.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now