将 https ip 重定向到共享主机中 htaccess 中的域

Posted

技术标签:

【中文标题】将 https ip 重定向到共享主机中 htaccess 中的域【英文标题】:Redirect https ip to domain in htaccess in shared hosting 【发布时间】:2021-03-19 02:18:51 【问题描述】:

Google 已使用 ssl 索引我的网站 IP 地址,因为我在我的网站上使用 ssl,我收到错误“错误您的连接不是私有的”。 我知道有很多关于*** 的问题,例如this one。

已编辑:首先我认为这段代码不起作用,但后来我明白下面的代码不会导致将 https://ip 重定向到主域。 它也适用于 http 并重定向所有子文件夹。我使用共享主机。

RewriteCond %HTTP_HOST ^111\.111\.111\.111$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

这个问题是否与我将此代码放在 htaccess 文件中的位置有关?我在我的网站上使用 joomla CMS。 我还使用以下代码将 http 和 www 重定向到 https 和非 www:

RewriteEngine on
RewriteCond %HTTP_HOST =mail.example.com
RewriteRule ^ https://example.com%REQUEST_URI [R=301,L]

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

谁能帮帮我?我找到的所有链接都是关于专用服务器的。

已编辑:我已在此处添加我的 htaccess 文件:

##
# @package    Joomla
# @copyright  Copyright (C) 2005 - 2017 Open Source Matters. All rights reserved.
# @license    GNU General Public License version 2 or later; see LICENSE.txt
##

##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line 'Options +FollowSymLinks' may cause problems with some server configurations.
# It is required for the use of mod_rewrite, but it may have already been set by your 
# server administrator in a way that disallows changing it in this .htaccess file.
# If using it causes your site to produce an error, comment it out (add # to the 
# beginning of the line), reload your site in your browser and test your sef urls. If 
# they work, then it has been set by your server administrator and you do not need to 
# set it here.
##

## No directory listings
<IfModule autoindex>
  IndexIgnore *
</IfModule>

## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes

## Mod_rewrite in use.
RewriteEngine on

RewriteCond %HTTP_HOST ^45\.138\.133\.59$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %HTTP_HOST =mail.example.com
RewriteRule ^ https://example.com%REQUEST_URI [R=301,L]

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


RewriteEngine On
RewriteCond %HTTPS off
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$


## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed 
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
# Block any script that includes a <script> tag in URL.
# Block any script trying to set a php GLOBALS variable via URL.
# Block any script trying to modify a _REQUEST variable via URL.
# Return 403 Forbidden header and show the content of the root home page
RewriteCond %QUERY_STRING base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %QUERY_STRING (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %QUERY_STRING GLOBALS(=|\[|\%[0-9A-Z]0,2) [OR]
RewriteCond %QUERY_STRING _REQUEST(=|\[|\%[0-9A-Z]0,2)
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.

## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects

##
# Uncomment the following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##

 RewriteBase /

## Begin - Joomla! core SEF Section.
#
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%HTTP:Authorization]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
# and the requested path and file doesn't directly match a physical file
# and the requested path and file doesn't directly match a physical folder
# internally rewrite the request to the index.php script
RewriteCond %REQUEST_URI !^/index\.php
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
<IfModule mod_expires.c>
  FileETag MTime Size
  AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month "
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
</IfModule>

## BEGIN EXPIRES CACHING - JCH OPTIMIZE ##
<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest "access plus 0 seconds"

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Data
  ExpiresByType text/xml "access plus 0 seconds"
  ExpiresByType application/xml "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"

# Feed
  ExpiresByType application/rss+xml "access plus 1 hour"
  ExpiresByType application/atom+xml "access plus 1 hour"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# HTC files (css3pie)
  ExpiresByType text/x-component "access plus 1 month"

# Webfonts
  ExpiresByType application/font-ttf "access plus 1 month"
  ExpiresByType font/opentype "access plus 1 month"
  ExpiresByType application/font-woff "access plus 1 month"
  ExpiresByType application/font-woff2 "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/font
AddOutputFilterByType DEFLATE application/font-truetype
AddOutputFilterByType DEFLATE application/font-ttf
AddOutputFilterByType DEFLATE application/font-otf
AddOutputFilterByType DEFLATE application/font-opentype
AddOutputFilterByType DEFLATE application/font-woff
AddOutputFilterByType DEFLATE application/font-woff2
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/woff
AddOutputFilterByType DEFLATE font/woff2
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
## END EXPIRES CACHING - JCH OPTIMIZE

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72___lsphp .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

【问题讨论】:

您的规则看起来正确。把它放在RewriteEngine On线下面 我相信你的htaccess规则RewriteCond %HTTP_HOST ^111\.111\.111\.111$RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]应该是.htaccess文件RewriteEngine ON下的前2条规则,你能不能试一试,然后让我知道它是怎么回事? 【参考方案1】:

能否请您在前两行(RewriteEngine ON 行之后)中使用您的规则。请尝试以下规则并确保在测试 URL 之前清除缓存。注意:我没有改变你的任何规则,我只是在这里改变了规则的顺序。

RewriteEngine ON
RewriteCond %HTTP_HOST ^111\.111\.111\.111$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %HTTP_HOST =mail.example.com
RewriteRule ^ https://example.com%REQUEST_URI [R=301,L]

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

RewriteCond %QUERY_STRING base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %QUERY_STRING (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %QUERY_STRING GLOBALS(=|\[|\%[0-9A-Z]0,2) [OR]
RewriteCond %QUERY_STRING _REQUEST(=|\[|\%[0-9A-Z]0,2)
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* index.php [F]
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%HTTP:Authorization]

RewriteCond %REQUEST_URI !^/index\.php
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_URI !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %REQUEST_URI !^/\.well-known/pki-validation/[A-F0-9]32\.txt(?:\ Sectigo\ DCV)?$
RewriteRule .* index.php [L]

【讨论】:

感谢您的 cmets,但不幸的是,通过更改规则的顺序并没有解决 https 的问题。我清除了缓存,甚至等待一天确定它是否有效。顺便说一句,我的网站是由 joomla 制作的,你认为组件或插件会导致这个问题吗?我已将完整的 htaccess 文件附加到我的问题中。现在111.111.111.111/something 没有重定向到example.com/something。但它适用于 http。 你好,我相信 joomla 最初没有被标记为有问题,我很抱歉,但我没有研究过它,否则会在这里尝试一些东西。如果可以的话,@anubhava 先生可能会在这里提供帮助。 亲爱的@RavinderSingh13,我认为我的规则有问题,与joomla无关。因为正如我所说,现在带有 http 的 myip/something 正确重定向到带有 https 的 example.com/something 。问题在于 myip 和 https,因为现在没有规则。我说的对吗? @Sarah,条件RewriteCond %HTTP_HOST ^111\.111\.111\.111$ 恕我直言应该可以工作,我需要检查这里可能出现的问题。可能其他 GURU 也可以跳到这个问题上。如果我在这里得到一些积分,会在这里发布,谢谢。

以上是关于将 https ip 重定向到共享主机中 htaccess 中的域的主要内容,如果未能解决你的问题,请参考以下文章

来自非安全服务器的HTTPS重定向在浏览器中显示错误消息

将 https 非 www 重定向到 https www 不工作的虚拟主机

将站点重定向到https后,站点IP重定向不起作用

如何用iptables将透明代理请求重定向另一主机

将 http 重定向到 https 排除特定 IP

Apache 2.4 将本地虚拟主机重定向到 https 而不是 http