.htaccess代码,用于将访问从IP地址重定向到域名



我注意到我可以使用 I.P 地址"134.209.16.153"访问我的网站"cbdshopy.co.uk",这会导致重复内容。

我想将 IP 访问重定向到域。

这是我目前的.htaccess,它100%强制https和非www

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END WordPress

我试图将以下行添加到我的 .htaccess 中,以强制对该域进行 IP 访问。

RewriteCond %{HTTP_HOST} ^134.209.16.153

喜欢这个

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^134.209.16.153
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END WordPress

但似乎没有解决问题。

有什么想法吗?

您的规则不起作用,因为您正在检查两个不同的主机标头www.example134.209.16.153。您只需要检查 IP 主机 .

这是更正后的版本:

#redirect ip to domain
RewriteCond %{HTTP_HOST} ^134.209.16.153
RewriteRule ^(.*)$  https://yoursite.com/$1 [R=301,L]
#www to non-www
RewriteCond  %{HTTP_HOST} ^www.(.+)$
RewriteRule (.*) https://%1/$1 [L,R=301]
#non ssl to ssl
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

确保在测试这些新重定向之前清除浏览器缓存。

最新更新