I am working on a website to display different products, users can access different product page by urls like 'mydomain.com/product/1.html', 'mydomain.com/product/2.html'
but I don't want to present those static html files directly, instead there is a php script will be loaded first at mydomain.com/product.php
so I want to redirect 'mydomain.com/product/1.html' to 'mydomain.com/product.php' but don't change its original url
I tried alias, try_files, return, but all of them rewrite the url to 'mydomain.com/product.php'
I am new to Nginx, this is probably a very simple question, but I have tried to achieve this for couple of days.
Best Answer
There are so many ways to do it...
The most simple is
at the server context. Your original request URI (i.e.
/product/1.html
) will be available forproduct.php
script as$_SERVER['REQUEST_URI']
array item value.You can also use
rewrite
directive at the location context, this can be slightly (very slightly) more performant:If you want, you can get product code and pass it to your
product.php
script as a query argument:or
This way your product code (
1
for the/product/1.html
URI,2
for the/product/2.html
URI, etc.) will be available forproduct.php
script as$_GET['product']
array item value.You can even define an individual FastCGI handler for this:
(original request URI will be available via
$_SERVER['REQUEST_URI']
), or if you want for product code to be available via$_GET['product']
: