0

We have currently a wildcard vhost accepting all domain request.

I want to redirect subdomain.domain.com to www.subdomain.domain.com so my wildcard will answer

One exception, I dont want to redirect www.domain.com to www.www.domain.com

How to achieve that ? I've put together (outside the wildcard block) but it's not dealing with the exception and not sure about the variable thing

if ($http_host ~ (.*).domain.com) {
    set $test P;
}

if ($http_host != www.domain.com) {
    set $test  "${test}C";
}

if ($http_host != www.*.domain.com) {
    set $test  "${test}D";
}

if ($test = PCD) {
    rewrite ^(.*)$ www.%1.domain.com$1 permanent;
}

Thanks

6
  • Try this rule using if, using if is not recommended if ($http_host = subdomain.domain.com) { rewrite ^(.*)$ www.subdomain.domain.com$1 permanent; } – serverliving.com Jul 27 '15 at 8:59
  • I know the domain but not the subdomain (there'll be 300 subdomains), how to add dynamic part to it ? – Tristan Jul 27 '15 at 9:08
  • Try this for Dynamic subdomains if ($http_host ~ (.*).domain.com) { rewrite ^(.*)$ www.%1.domain.com$1 permanent; I have not tried this, but it should work – serverliving.com Jul 27 '15 at 9:12
  • Question edited. I end up with 414 URI too long with url like : http://www.subdomain.domain.com/www.domain.com/www.domain.com/....... – Tristan Jul 27 '15 at 9:56
  • Try this if ($http_host ~ ^([a-zA-Z0-9]+).domain.com$) { rewrite ^(.*)$ www.%1.domain.com$1 permanent; – serverliving.com Jul 27 '15 at 10:09
1

I would use this:

server {
    listen 80;
    server_name www.example.com example.com;

    # this is default server.
    ...
}

server {
    listen 80;
    server_name www.*;

    # here www.*.example.com
    ..
}

server {
    listen 80 default_server;
    return 301 http://www.$host$request_uri;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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