大网数据广告活动
网站运营与故障问答

Access Website by domain name only (not IP address)

· 52CE 我爱测

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 h…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。