I was wondering how I can make the URL of my website different from the path of the file in my website directory.
For example, let's say I want my website language to be in the URL (mywebsite/en/index.php) but I don't want to manually have to create each php file in my website directory containing just an include from my main files, how could I do that please? I'm using Nginx if needed
CodePudding user response:
You can use nginx rewrite regex rules. Nginx rewrite rules
Let's say you want the url below to hit index.php with the language code.
Url: https://yourwebsite.com/en/index.php
FilePath: /path/to/www/index.php
rewrite ^/(.*)/index.php$ /index.php?lang=$1 last;
# another example
# $1 is the matching characters in regex `(.*)`
rewrite ^/sales/(.*)/categories.php$ /some/path/cat.php?categories=$1 last;
Do not forget. All URL's are imaginary but all files are real. URLs do not need to match the real files.
URL: https://yourwebsite.com/index.php
may hit /some.file.asp
Look at the official nginx documentation for more details.
Hope, it helps.