I created a website based on ajax tech. and now I want to achieve similar effects using php (nginx). The problem is I don't want to create (even dynamically) .php documents for every user or offer or anything. I have templates for every pages like userTemplate.php and it works fine with $_GET method, which fill it with proper data BUT the address is not acceptable, hoe can I convert that requestexample.com/userTemplate.php?id=1 to example.com/users/1. I tried with rewriting it in config files, but then it redirects me to the path /users/1 which is ofc wrong... Is it possible? Please I've already spent hours looking for any answer.
1 Answer
Try this:
location / {
try_files $uri $uri/ =404;
}
location ~* /users/(\d+) {
try_files $uri /userTemplate.php?id=$1;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
#
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
If you want to go for an alternative approach, have a look at Creating NGINX Rewrite Rules. If you need different, more complicated regular expressions, perhaps start here.
code location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; }What should be there to get it works?