如何将ip重定向到域名?

How to redirect ip to domain name?

我使用 Apache 网络服务器,我想将所有 url 从 ip 重定向到域。这是我的网站 .htaccess 文件的内容:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\s\S]*)$ index.php?rt= [L,B,QSA]

根据一些研究,我想我需要这样的东西:(不确定)

RewriteBase /
RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^([\s\S]*)$ https://lamtakam.com/ [L,R=301]

但我不知道应该如何(在哪一部分)将这些三行添加到 .htaccess 文件中。有什么线索吗?

您应该在 https/www 重定向规则之前或之后插入 IP 重定向规则:

Options -Indexes
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^ https://lamtakam.com%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php?rt= [L,B,QSA]

如果你想让你的 IP 匹配条件更通用,那么使用:

RewriteCond %{HTTP_HOST} ^\d+\.\d+\.

能够匹配任何 IP 地址。