1

Could you please help me to figure out the following redirection using Nginx?

When a user type http(s)://(www.)domainA.com/view.php he/she will be able to access this view.php file (which is in web root)

All other requests which are coming to http(s)://(www.)domainA.com should be redirected to https://learn.domainB.com

(Basically no redirection should be happened only for this view.php file)

My current Nginx vhost file:

server {

    listen 80;
    server_name domainA.com www.domainA.com;

    root /var/www/public_html;

    if ($request_uri !~* (/view.php) ) {
       rewrite ^ https://learn.domainB.com permanent;
    }
    location /view\.php {
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index view.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }
}

I really appreciate your inputs. Thanks!

2 Answers 2

0

I would prefer this configuration:

server {

    listen 80;
    server_name domainA.com www.domainA.com;

    root /var/www/public_html;

    location = /view.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index view.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
       return 301 https://learn.domainB.com;
    }
}

There is no need for if statement, since nginx processes location orders in from start to end, and uses the first matching location block.

This doesn't help with the problem of view.php getting downloaded, there is something on the file that prevents it from getting executed just like Anubioz mentioned.

I also removed the escaping of dot in the location = /view.php part, since this is not a regular expression match, and therefore the escape is not necessary.

0
0

You configuration should work just fine, though I'd replace rewrite with 301 redirect:

if ($request_uri !~* (^/view.php$) ) {
    return 301 https://learn.domainB.com;
}

If you need to pass any parameters to view.php (like view.php?a=b) then your configuration should look like this:

if ($request_uri !~* (^/view.php(\?.*)?$) ) {
    return 301 https://learn.domainB.com;
}
2
  • Hello Anubioz, Thanks a lot for your quick reply. Actually, "All other requests which are coming to http(s)://(www.)domainA.com should be redirected to learn.domainB.com" is working fine. When I type http(s)://(www.)domainA.com/view.php it tryes to download the php file instead of executing it. (php files are executing fine with other vhosts hosted in the same server. So I support this is not due to a missing php module) Aug 15, 2016 at 4:11
  • And what exactly is different from those "other vhosts" which work fine? I'd say the problem is in view.php file itself. Have you forgotten to enable short_tags in php.ini? Can you confirm that view.php moved to another vhost works fine? What about moving some other php file on place of view.php?
    – Anubioz
    Aug 15, 2016 at 4:20

You must log in to answer this question.

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