0

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.

2
  • IMHO, it's way more likely that you'll get an answer, if you add your existing nginx config to your Question. Commented Feb 25, 2017 at 23:03
  • Ok, but now it's pure: 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? Commented Feb 25, 2017 at 23:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! It solved my problem and finally worked! I only had to add "$uri after try_files" :)
Right, added $uri

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.