Home>Article>Backend Development> How to hide .php in url
Method to hide ".php" in URL: First find and open the "nginx.conf" configuration file; then add the content "location / {ttry_files $uri $uri/ $uri.php$is_args$args; }"; Finally save the changes.
# Recommended: "
PHP Video Tutorial》
Nowadays, many people like to use nginx as a web server to deploy websites. nginx is also very convenient to configure. If your website is developed in PHP language, let’s teach you a simple trick below. Quickly hide the php suffix of the web page and implement a Pretty URL.
Easy to use nginx’s try_files directive.
location / {ttry_files $uri $uri/ $uri.php$is_args$args;}to hide the .php suffix name of the web page. try_files is an instruction in nginx, somewhat similar to rewrite, which is used to try to find the file or directory with the path specified after the instruction and return it. $uri represents the URI path of the current request, which is the path behind the URL domain name, without parameters. $ is_args means that if there are parameters in the requested URL, $is_args represents "?". If there are no parameters in the requested URL, an empty string will be returned for parameter passing. $args represents the complete parameters in the request URL. The following is an explanation of how the nginx server handles it based on the example in the figure:
Scenario 1:
Assume that the URL visited by the user is: yuanhuaixuetang .com/aboutnginx server will first try to find the about file (relative to the website root directory), and if it exists, return the fileIf the about file does not exist, try to find about / directory (relative to the website root), returned if it exists.
If the about/ directory does not exist, try to find the about.php file (relative to the website root directory). If it exists, return the file and bring the subsequent request parameters according to the request information. In this example, the actual return is about .php, because the original request does not take parameters, so the actual return does not take parameters.If none of the above are found, you can return the corresponding error message according to the configuration in nginx.
Scenario 2:
Assume that the URL visited by the user is: yuanhuaixuetang.com/about?id=12
The parsing process of ginx is as follows:
The URL has passed parameters, ##is_args is "?", $args is id=12The parsing process is the same as above and will not be described in detail , what is actually returned to the user in this situation is: access the URL of the domain name about.php?id=12.
The above is the detailed content of How to hide .php in url. For more information, please follow other related articles on the PHP Chinese website!