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!
You configuration should work just fine, though I'd replace rewrite with 301 redirect:
If you need to pass any parameters to
view.php
(likeview.php?a=b
) then your configuration should look like this:I would prefer this configuration:
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.