Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following mod_rewrite rule

RewriteCond %{HTTP_HOST} !^(host)\.doamin\.com [NC]
RewriteCond %{HTTP_HOST} !^(www)\.domain\.com [NC]
RewriteRule ^(.*) /magento/$1 [L]

I need that to work in nginx and I've been slamming my head against the wall to make it work



Here is a better explanation of what I'm trying to do

so a user can go into and the url will stay there

We do this in apache like

RewriteCond %{HTTP_HOST} !^(host)\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^(www)\.domain\.com [NC]
RewriteRule ^(.*) /magento/$1 [L]

We also have a dns catchall that will send all * to a default apache vhost. I'm slamming my head against the wall to get it working in nginx to keep the hostname the same but behind the scenes rewrite the domain for magento to parse.

Here is what I have but it just keeps appending /magento/magento/magento to the end till it kills the loop

    if ($http_host !~ "^www.domain\.com$") {
        rewrite ^.+ http://$http_host/magento/$uri last;

The problem with the above is it keeps rewritting it as and so on

Here is a error log for it

2009/11/03 15:40:26 [error] 22347#0: *2 rewrite or internal redirection cycle while processing "/magento//magento//magento//magento//magento//magento//magento//magento//magento//magento//magento//catalogsearch/advanced/result/", client:, server: laptop, request: "GET /catalogsearch/advanced/result/?featured=1 HTTP/1.1", host: ""
share|improve this question
up vote 1 down vote accepted

Try something like this:

    listen 80;
    server_name * ;
    rewrite ^.+ httр://$host/magento/$uri last;

You also can exclude hostnames and already done redirects via location+if+regex inside server {} section

location !~ \/magento\/
    if ($host !~ "^(www|host)\.domain\.com$")
    // Here goes your rewrite

PS. I think this could be done more sexy using 2 server {} blocks, but I'm too lazy to think about it now =)

share|improve this answer
that got me 50% of the way there. I need it to ignore and and keep the url if you go to it doesn't get redirected to i need to pass the uri to /magento/ and keep the hostname there – Mike Nov 3 '09 at 19:58
Check now. Is it suitable for you? – SaveTheRbtz Nov 3 '09 at 20:15
Thanks.. its getting there.. I updated my post to better describe my situation. thanks for the help – Mike Nov 3 '09 at 20:53
Author of nginx strongly disapproves constructs like if ($host !~ ... ) Don't bother with apache rewrites. Just state your problem in simple terms. AFAIU, you need: stays the same rewrites to am I right? Is that all there is? – squadette Nov 11 '09 at 16:51
You need to redirect every subdomain except host and www. I've already said that the right move will be making 2 server {} directives one matching ~ ^(www|host)\.domain\.com$ and other !~ ^(www|host)\.domain\.com$ but it'll be less customizeble i.e. you can't add any other domain to such configuration. – SaveTheRbtz Nov 12 '09 at 14:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.