tim peterson
tim peterson

Reputation: 24305

subdomains using NGINX and Codeigniter

I'd like to have various subdomains on my site such as: biology.mysite.com.

Currently I have the following in my nginx.conf:

server {
  listen 80;
  server_name biology.mysite.com;
  return 301  https://mysite.com/tags/biology$request_uri;
}

This code works in that if you point the browser to biology.mysite.com, it redirects to https://mysite.com/tags/biology and the correct biology content loads.

However, I don't want to redirect the url but want to keep the subdomain as the url.

So I replaced the above with the code below based on this gist but it has no effect (meaning pointing to biology.mysite.com simply goes to mysite.com):

      if ($host ~* ^www\.([a-z0-9]*)\.(mysite\.com) )
      {
          set $tagname $1;
          set $host_without_www $2;
          rewrite ^/(.*)$ $scheme://$tagname.$host_without_www/ permanent;
      }

      if ($host ~* ^([a-z0-9]+)\.(mysite\.com))
      {
          set $tagname $1;
            set $host_without_www $2;

            rewrite ^/$ /index.php/tags/index/ last;
            rewrite ^/(.+)$ /index.php/$1 last;
      }

Thoughts?

Note the reason I posted this here rather than Serverfault is b/c I'm using the Codeigniter framework (which has a unique routing scheme) and so the answer might involve knowledge of NGINX and Codeigniter.

Upvotes: 2

Views: 2018

Answers (1)

Alexander Altshuler
Alexander Altshuler

Reputation: 3064

I believe your last rewrite should looks like below:

rewrite ^/(.+)$ /index.php/tags/$tagname$1 last;

UPDATE: If you want to redirect requests like subdomain.mysite.com (WITHOUT uri part) to tags controller and use subdomain method you should change line below

rewrite ^/$ /index.php/tags/index/ last;

to

rewrite ^/$ /index.php/tags/$tagname/ last;

lynxluna's nginx configuration and comment in mentioned gist is wrong.

Upvotes: 1

Related Questions