问题原文
原帖链接 · amit singh · CC BY-SA 3.0
Let's say I have a website example.com and it points to 12.23.42.31. Now I want that nobody can access my website by the IP address (12.23.42.31). What is the procedure to achieve this?
LAMP server with WordPress. Not shared hosting.
已采纳解答
原帖链接 · Tom Brossman · CC BY-SA 3.0
Because you mentioned this is for a LAMP server and you have SSH access, you can do this quite simply with a 000-default VirtualHost like this, (changing example.com to your domain):
<VirtualHost *:80>
Redirect permanent / http://www.example.com/
</VirtualHost>
Rewrite methods are unnecessary, and redirecting provides better security against host header attacks. By skipping the usual .htaccess or PHP methods and doing a redirect you get the best possible performance, though this probably isn't a concern if most requests are for your domain and not the IP.
For completeness, here's how you do it with Nginx:
server {
listen 80 default_server;
return 444;
}
One slight difference, Nginx's 444 response means 'no response' and it immediately drops the connection. In the unlikely event that you would want to use a redirect instead you can change the return 444 directive to return 301 http://www.example.com/.
It is really unlikely that legitimate traffic from your users is requesting your site by it's IP address, so don't waste much time trying to accommodate it. Drop or redirect the traffic instead.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
