This should be really easy to do but I'm hitting my head on the wall. If I get a request for www.mysite.com/mypath I want to serve the content of www.mysite.com/myotherpath/thisfile.html. How can I do this with an nginx config.
Asked
Active
Viewed 2.8k times
2 Answers
15
location = /mypath {
try_files /myotherpath/thisfile.html =404;
}

VBart
- 14,714
- 4
- 45
- 49
-
This doesn't work if the target is a PHP file. – Michael Chourdakis Feb 10 '22 at 14:22
-
How would one make this work for a PHP file? – Jack Avante Feb 28 '23 at 15:02
9
Use rewrite directive within proper location block. So for example you have basic location which will handle all requests
location / {
/*your rules here*/
}
You will need to add another block, which will do for you handling of specific path
location /mypath {
rewrite ^/mypath$ /real/path/to/file/thisfile.html;
}
Also for your server to think in that block that thisfile.html
is default you can use try thisfile.html
directive
It is all well explained on official page Official Nginx RewriteModule page

VBart
- 14,714
- 4
- 45
- 49

Alexey Kamenskiy
- 2,888
- 5
- 36
- 56
-
it's a bad way of doing things in nginx, please, see here: http://wiki.nginx.org/Pitfalls – VBart Sep 10 '12 at 19:59
-
@VBart I did not find anything related to handling of specific URI on that page. – Alexey Kamenskiy Sep 11 '12 at 17:52