In actual development, sometimes we need to hide the domain name after going online to avoid being attacked. Developers using the ThinkPHP framework can use some methods to achieve this goal.
Idea: Hide the actual URL address by defining a routing rule.
The steps are as follows:
(1) Modify the project configuration file (config.php) and define a routing rule:
'路由规则' => '目标地址'
For example:
'test' => 'index/index/hello'
It means that when accessing "http://domain name/test", jump to the "hello method of index controller index operation".
(2) Enter "http://domain name/test" in the browser address bar, and you will see that the actual URL address has been hidden.
Idea: Use pseudo-static technology to convert the URL address into a search engine-friendly form, and also have the effect of hiding the URL address.
The steps are as follows:
(1) Configure pseudo-static on the Apache server and place the ".htaccess" file in the root directory of the website.
(2) Add the following code in the "index.php" file of the project:
if (!is_file('./html' . $_SERVER['REQUEST_URI'] . '.html')) { // 生成html文件 ob_start(); // 渲染页面 $content = ob_get_clean(); // 保存为静态文件 file_put_contents('./html' . $_SERVER['REQUEST_URI'] . '.html', $content); } // 输出静态文件 echo file_get_contents('./html' . $_SERVER['REQUEST_URI'] . '.html');
means: if the corresponding HTML file exists, output the file; otherwise, in "html "Generate a corresponding static HTML file in the directory and output the content.
(3) Enter the URL address corresponding to the page in the browser address bar, and you will see that the actual URL address has been hidden.
Idea: Use reverse proxy technology to use an address as a representative and forward all requests to this address. At the same time, on the representative address Configure reverse proxy rules to forward requests to the real address.
The steps are as follows:
(1) Configure reverse proxy rules on the Nginx server and add the following code to the "/etc/nginx/nginx.conf" file:
server { listen 80; server_name 域名; location / { proxy_pass http://真实的地址; proxy_set_header Host $host; } }
It means: forward all requests to the real address, and set the "Host" header parameter in the target address to the domain name of the current host.
(2) Enter the representative address in the browser address bar to access the real address, and the actual URL address has been successfully hidden.
Summary
Based on the above three options, we can choose which method is more suitable for our project based on actual needs. Through the above methods, we can not only ensure the security of website operation, but also provide users with a better access experience.
The above is the detailed content of How to hide domain name after thinkPHP goes online. For more information, please follow other related articles on the PHP Chinese website!