2

We facing a problem and we can't fix it. We are currently able to run our application as expected - SEO URL runs like a charm and are rewriting to index.php. The problem is, we have an additional URL Pattern like:

  • /controllerPrefix/a-param.php
  • /controllerPrefix/an-other-param.php
  • /controllerPrefix/an-other-other-param.php

...which we need to rewrite to index.php too. We are unable to write this URL dynamic URL which ends in .php. Please note that there are no physical PHP files e.g. a-param.php -> it's a dynamic pattern which ends in .php. Unfortunately, the output in browser is File not found. The following error is logged in nginx 1.10:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream...

-> 404 not found

NGNIX Configuration:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

How to rewrite this dynamic URL ending in /controllerPrefix/*.php to index.php?

lin
  • 123
  • 4

1 Answers1

2

You have two options:

Either 1) redirect all URIs ending with .php to /index.php, if there is no matching script file - add a try_files statement to the existing location ~ \.php$ block (see this document for details):

location ~ \.php$ {
    try_files $uri /index.php?$args;
    ...
}

Or, 2) redirect URIs that begin with /controllerPrefixng to /index.php - add a new location block (see this document for details):

location ^~ /controllerPrefix/ {
    rewrite ^ /index.php last;
}
Richard Smith
  • 11,859
  • 2
  • 18
  • 26