mod_rewrite: Problems with excluding ~dir

Hello!

There are two things that I'm trying to do:

1) Redirect all requests for "host-1-01" to "vm-1-01"

2) Unless a user requests "/~dave" in "host-1-01.example.net/~dave"

  RewriteEngine on
        RewriteCond %{HTTP_HOST} ^host-1-01\.example\.net$
        RewriteCond %{HTTP_HOST} ^host-1-01\.example\.net\/~dave$
        RewriteRule ^(.*)$ http://vm-1-01.example.net [L,R]

Currently, step 1 only works if the second ~dave RewriteCond is commented out, and step 2 doesn't work unless step 1 is commented out (can't say it's really "working" though if it isn't redirected by default).

Much obliged for any help on this one.

2 Replies

I'm no mod_rewrite maven, but shouldn't your second condition exclude requests ending with ~dave?

I think you can the not operator:

RewriteCond %{HTTP_HOST} !^host-1-01\.example\.net\/~dave$

Turns out it required the REQUEST_URI variable, so:

RewriteCond %{HTTP_HOST} ^host-1-01\.example\.net$
RewriteCond %{REQUEST_URI} ^host-1-01\.example\.net\/~dave
RewriteRule ^(.*)$ http://vm-1-01.example.net [L,R]

Cheers!

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct