0

I need to rewrite to another host, unless the url starts with /store or /utils

I have tried looking at the apache docs, but I can't work out how to do this

3 Answers 3

4

Ok assuming you want to redirect to a host otherhost.example.com this should do the trick under Apache.

RewriteCond %{REQUEST_URI}    !^/(store|utils)/.*
RewriteRule (.*)    http://otherhost.example.com$1 [R=301,L]

The conditional part says "if the request URI doesn't begin with /store/ or /utils/ apply the Rewrite. The rewrite rule will then generate a 301 redirect with the captured URI appended to the request.

1
  • Kudos for actually specifying R=301 too
    – Frenchie
    Feb 11, 2010 at 3:11
1

As ickymettle succintly responded, the ! flag signifies it should consider the input as matched if the conditions of the rule are NOT met. So, breaking down the syntax:

! = 'Not' prefix
^ = start of string
/ = The first forward slash tells the rule to match only if the input string
    begins with a /
| = the pipe symbol denotes 'or' inside the bracketed group section
    (as in 'store or utils')
    The second forward slash denotes that the string ends with a forward slash
. = any single character
* = zero or more of the preceding character (so .* = 0 or more of any character, effectively passing on the rest of the URI

The AddedBytes.com mod_rewrite cheatsheet is dead handy, I have it taped to the wall next to where I sit (as I dive into htaccesses almost daily and desperately try to teach myself mod_rewrite voodoo!) There's also a [great detailed article on PerishablePress with loads of tips, tricks etc. The one nice thing about mod_rewrite is that it isn't deprecated with new releases constantly, so old articles on the topic remain perfectly valid :-)

Might as well go for broke and also mention [the mod_rewrite / .htaccess cheatsheet on TheJackol, which has some useful quick code snippets and a few bits of info.

Other URLs:

  • perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/
  • thejackol.com/htaccess-cheatsheet/

Sorry I couldn't hyperlink them, I don't have 10 Rep points yet so the system blocked me from hyperlinking all three :-(

0
RewriteCond URL (?!.*/utils).*
RewriteCond URL (?!.*/store).*
RewriteRule (.*) http\://{your new host here}/$3 [I,R]

Note: This is the syntax for ISAPI_Rewrite, which should be compatible with mod-rewrite.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .