Forum Moderators: phranque

Message Too Old, No Replies

Redirecting all but a specific URL

Using RedirectMatch (mod_alias)

         

bsterz

2:29 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



Greetings!

I need to redirect ALL pages on a site except ONE URL.

So if a request comes in for /test.html I want to do nothing and serve the page as requested, if ANY OTHER request comes in, I want to redirect to example.com

I tried:

RedirectMatch!(/test\.html) http://www.example.com

But this does nothing for either pages called /test.html or for the other pages.

Thanks for any help.

Bill

Birdman

5:10 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think mod_rewrite is the best way to do the redirect here, because you can use the RewriteCond directive with it.

RewriteEngine On
RewriteCond %{REQUEST_URI}!^/test\.html$
RewriteRule (.*) [anotherdomain.com$1...] [R=301,L]

I believe that's it there.

PS: make sure to add a space between URI} and!^/
the system takes them out for some reason.

bsterz

5:17 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



Birdman,

Thanks for the reply, but I need to do this with mod_alias as this server does not have mod_rewrite installed.

Bill

Birdman

6:22 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I see. I'm not the regular expression whiz around here, but I think this may do the job:

RedirectMatch ^/(?!test\.html) http://www.example.com

Which means a slash, which is not followed directly by test.html. By leaving off the end anchor($), it will match anything besides /test.html. Hopefully ;)

jdMorgan

6:23 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid you're stuck then. The ability to rewrite or redirect based on NOT(Some condition) was a new feature added with mod_rewrite, and not available in mod_alias.

There are complex solutions based on forwarding all server requests to a script that will handle the serving and redirection of requests, if you really want to get into that instead of looking for a host that will support your needs...

Jim

Birdman

6:37 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was afraid that the negative lookahead (?!) wouldn't be supported by Apache. Sorry bsterz!

bsterz

6:47 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



Bummer.

Thanks guys, at least this keeps me from banging my head against the wrong wall :)

Bill