Score:0

PHP website not working as expected in NGINX but working in Apache

cn flag

Hi I just created a link shortening app. But when I try to redirect a shorten link to the full URL which is shared on Facebook it is not working as expected. for example : https://bowa.me/c8443 this link is working fine but if i share the link in Facebook and and the link will be like this https://bowa.me/c8443?fbclid=IwAR0Zm8bGRgrbpQTUX_aVXxTMNFq6-MlRFe0j8e_7wm4anbWmvArPlyDaAHI This link is not redirecting

nginx config

location / {
            try_files $uri $uri/ /index.html /index.php;

    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         }

        location ~ /\.ht {
            deny all;
      }

    if (!-e $request_filename) {
            rewrite ^/admin/(.*)?$ /admin/index.php?a=$1 break;
            rewrite ^/(.*)$ /index.php?a=$1 last;
            break;
    }
Michael Hampton avatar
cz flag
You should be using `try_files`, not `if (!-e $request_filename) ...`
cn flag
I am using 'if (!-e $request_filename) ..' because previously it was downloading the page instead of redirecting. I have found this solution in a forum. the first link I have shared working perfectly. but the second one is not. and both are working in apache (xampp).
Score:1
us flag

In nginx, things should be done by following nginx best practices, not trying to convert Apache2 practices to nginx. That is a recipe for all kinds of problems.

You should try the following approach:

# block for processing PHP files
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

# Capture part after admin to variable and use in try_files
location ~ ^/admin/(.*)$ {
    try_files $uri $uri/ /admin/index.php?a=$1;
}

# Default location, capture URI part and use as argument
location ~ ^/(.*)$ {
    try_files $uri $uri/ /index.php?a=$1;
}

The order is important, nginx uses the first regular expression match from location blocks it finds.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.