Rewrite rule needed for subdomains

We have a lot of magento stores that work like

store1.domain.com
store2.domain.com

so a user can go into store1.domain.com/products/ 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 *.domain.com 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;
        break;
    }

Thanks!

On Tue, Nov 03, 2009 at 09:03:22PM +0100, Mike S. wrote:

RewriteCond %{HTTP_HOST} !^(host)\.domain\.com [NC]

to the end till it kills the loop

    if ($http_host !~ "^www.domain\.com$") {
        rewrite ^.+ http://$http_host/magento/$uri last;
        break;
    }
server {
    # a catchall server for "listen 80",
    # since it's "default" server for "listen 80"

    listen 80 default;
    server_name  store1.domain.com;

    location / {
        rewrite ^   http://$host/magento/$request_uri? last;
    }

    location /magento/ {
        ...
    }
}

server {
    listen 80;
    server_name  www.domain.com  host.domain.com;
    ...
}

Igor S. wrote:

On Tue, Nov 03, 2009 at 09:03:22PM +0100, Mike S. wrote:

RewriteCond %{HTTP_HOST} !^(host)\.domain\.com [NC]

to the end till it kills the loop

    if ($http_host !~ "^www.domain\.com$") {
        rewrite ^.+ http://$http_host/magento/$uri last;
        break;
    }
server {
    # a catchall server for "listen 80",
    # since it's "default" server for "listen 80"

    listen 80 default;
    server_name  store1.domain.com;

    location / {
        rewrite ^   http://$host/magento/$request_uri? last;
    }

    location /magento/ {
        ...
    }
}

server {
    listen 80;
    server_name  www.domain.com  host.domain.com;
    ...
}

That works but it changes the browser URL and adds /magento/ in there
and that can’t happen

When I go to http://store1.domain.com/products/

it gets changed to http://store1.domain.com/magento/products/

I just need the /magento/index.php to parse it via the URI

On Tue, Nov 03, 2009 at 10:19:09PM +0100, Mike S. wrote:

server {
    # a catchall server for "listen 80",
    # since it's "default" server for "listen 80"

    listen 80 default;
    server_name  store1.domain.com;

    location / {
        rewrite ^   http://$host/magento/$request_uri? last;

Probably, you need

  •           rewrite ^   http://$host/magento/$request_uri? last;
    
  •           rewrite ^   /magento$uri  last;

Igor S. wrote:

On Tue, Nov 03, 2009 at 10:19:09PM +0100, Mike S. wrote:

server {
    # a catchall server for "listen 80",
    # since it's "default" server for "listen 80"

    listen 80 default;
    server_name  store1.domain.com;

    location / {
        rewrite ^   http://$host/magento/$request_uri? last;

Probably, you need

  •           rewrite ^   http://$host/magento/$request_uri? last;
    
  •           rewrite ^   /magento$uri  last;
    

When I do that and hit the following url

http://www.domain.com/magento/shop/by/expert

I see the following in the log

2009/11/04 13:07:38 [error] 22578#0: *1 rewrite or internal redirection
cycle while processing “/magento/magento/shop/by/expert”, client:
127.0.0.1, server: _, request: “GET /magento/shop/by/expert HTTP/1.1”,
host: “www.domain.com”, referrer: “http://www.domain.com/

Here is my config

#######################################################################

This is the main Nginx configuration file.

More information about the configuration options is available on

* the English wiki - Welcome to NGINX Wiki! | NGINX

* the Russian documentation - nginx

#######################################################################

#----------------------------------------------------------------------

Main Module - directives that cover basic functionality

Module ngx_http_core_module

#----------------------------------------------------------------------

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

#----------------------------------------------------------------------

Events Module

http://wiki.nginx.org/NginxHttpEventsModule

#----------------------------------------------------------------------

events {
worker_connections 1024;
}

#----------------------------------------------------------------------

HTTP Core Module

Module ngx_http_core_module

#----------------------------------------------------------------------

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] 

“$request” ’
'$status $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

upstream backend {
server 127.0.0.1:9000;
}

#
# The default server
#
server {
    listen       80;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/vhosts/domain.com/html;
        index  index.php;

if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
  expires max;
}
    }

location /magento/ {
# rewrite - if file not found, pass it to the backend
if (!-f $request_filename) {
rewrite ^ /magento$request_uri last;
break;
}
}

    # pass the PHP scripts to FastCGI server listening on 

127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/vhosts/domain.com/html/$fastcgi_script_name; # same path as
above

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

}

On Thu, Nov 5, 2009 at 1:10 AM, Mike S. [email protected] wrote:

Here is my config

of course it will be cyclic. You put the rewrite in wrong location.
also, if ( ) is evil.