Reputation: 4698
I want nginx to rewrite the url to specific php files that can be determined by the content before the first slash
For example:
testing.com/test
or test.com/test
would be rewritten to test.php
testing.com/test2/variable/another-variable
would be rewritten to /test2.php?q=variable/another-variable
I would then use PHP to explode the q GET
parameter.
What I currently have tried is:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
This works for example 1 I have displayed above, but returns a 404 for example 2 with a more complicated URL.
Upvotes: 1
Views: 2377
Reputation: 49692
You can use a named location
with the try_files
directive to implement one or more rewrite
statements. See this document for details.
For example:
location / {
try_files $uri $uri/ $uri.html @php;
}
location @php {
rewrite ^(/[^/]+)$ $1.php last;
rewrite ^(/[^/]+)/(.*)$ $1.php?q=$2 last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
The rewrite
statements are evaluated in order. The second try_files
statement ensures that the PHP file actually exists and is to avoid passing uncontrolled requests to PHP.
Upvotes: 2