实施重定向以保护网站

Implement a redirect to Secure to website

实施到网站的 HTTPS 重定向

之前我们有一个网站是HTTP的,最近我们已经购买了HTTPS。现在我尝试实现重定向到 HTTPS。

为此,我在

中尝试了以下代码
    <IfModule mod_rewrite.c>
    ## enable rewrites
    
    Options +FollowSymlinks
    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ www.example.com/ [R,L] 
     
    </IfModule>

当我用上面的代码上传 .htaccess 文件时。网站无法显示。

我的整个 .htaccess 文件如下。

    #DirectoryIndex index.html

#AddType application/x-httpd-php5 .html .htm
#AddType application/x-httpd-php .html .htm
#RemoveHandler .html .htm
#AddType application/x-httpd-php .html .htm
#RewriteRule ^/?inscription/map\.html$ - [F,L]

<FilesMatch "\.html$" >
    #ForceType application/x-httpd-php
</FilesMatch>

#Canonicalization  issue
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ www.example.com/ [L,R=301]

#Google Caching Issue
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.example.com/ [R,L]


<IfModule mod_rewrite.c>
## enable rewrites

Options +FollowSymlinks
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ www.example.com/ [R,L]

#Redirect from http to https 
#RewriteCond %{HTTP:X-Forwarded-Proto} !https
#RewriteCond %{HTTPS} off
#RewriteRule ^ www.example.com/ [L,R=301]


ErrorDocument 404 www.example.com     
 
 
</IfModule> 

<IfModule mod_expires.c>
 ExpiresActive on
 ExpiresDefault "access plus 1 month"
 ExpiresByType application/javascript "access plus 1 year"
 ExpiresByType image/x-icon "access plus 1 year"
 ExpiresByType image/jpg "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType text/css "access plus 1 month"
</IfModule>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>


<Files "phpinfo.php">
     
    Order Allow,Deny
    Deny from all
</Files>

#<Files >

#AddType application/x-httpd-php .html .htm

#</Files>

Options -Indexes

你能帮忙解决一下吗?实现从 http 到 https 的重定向。

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.example.com/ [R,L]

您需要包含方案,即。 https 替换字符串 中。例如:

RewriteRule ^(.*)$ https://www.example.com/ [R,L]

这同样适用于您 .htaccess 文件中的所有其他重定向。

如果您没有在 HTTP 到 HTTPS 重定向中明确包含 HTTPS 方案,那么它永远不会重定向到 HTTPS。但是,相对路径 substitution(即不以方案或斜杠开头的路径)被视为相对于当前目录(除非定义了 RewriteBase)。因此,像 www.example.com 这样的替换字符串将被有效地视为当前目录(文档根目录)的子目录,并且尝试将其转换为外部重定向将导致格式错误的重定向,如 http://www.example.com/path/to/public_html/www.example.com/<foo>.

ErrorDocument 404 www.example.com  

您的 ErrorDocument 指令也有类似的问题。但是,您不应该“重定向”到自定义错误文档(如果您在此处指定绝对 URL 就会发生这种情况)。您应该定义一个自定义错误文档。例如。 /errors/my404.html 并在 ErrorDocument 指令中声明:

ErrorDocument 404 /errors/my404.html

然后使用内部子请求提供自定义错误文档。这里没有“外部重定向”。


更新#1:

Can you please edit above .htaccess file and post here?

我删除了所有 commented-out 代码部分 - 因此请酌情将其添加回来。尽管我认为被注释掉的 mod_rewrite 部分中的几个只是 earlier/incorrect 尝试。我还重新排序了一些位(例如,在文件的早期定义 OptionsErrorDocument 指令更合乎逻辑 - 你当然不应该将它们分开)。为简洁起见,我省略了 mod_expires 和 mod_deflate 部分。

我还为 Apache 2.4 Require all denied.

更新了已弃用的 mod_access_compat(OrderDeny 等)指令

我还假设 www 子域是规范的并且您没有其他子域。

# Allow FollowSymlinks (required for mod_rewrite)
# and prevent directory listings (mod_autoindex)
Options +FollowSymlinks -Indexes

# Define custom error documents
ErrorDocument 404 /errors/my404.html     

# Block access to specific files
<Files "phpinfo.php">
    Require all denied     
</Files>

# Enable mod_rewrite rewrite engine
RewriteEngine On 

# Canonical redirect: non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/ [R=301,L]

# Canonical redirect: HTTP to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.com/ [R=301,L]

# mod_expires directives go here...

# mod_deflate directives go here...

在测试之前清除浏览器缓存,并首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。

请注意,我假设您已经测试了 SERVER_PORT 80 检查并且这在您的服务器上按预期工作*1。我注意到在您的 commented-out 代码中您还检查了 X-Forwarded-Proto HTTP 请求 header - 只有当您的应用程序服务器位于管理 HTTPS 连接的 front-end 代理后面时才需要这样做.如果是这种情况,那么检查 SERVER_PORT(或 HTTPS)可能会失败。


*1 UPDATE#2: 看来不是这样的你必须实施 non-standard 检查 %{ENV:HTTPS} 而不是检查 SERVER_PORT,这会导致重定向循环。这意味着您的虚拟主机(我假设是 shared 托管平台)正在使用某种 front-end 代理来管理 SSL 连接,并且您的应用程序服务器实际上是通过纯 HTTP 与front-end 代理。

# Force from http to https 
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

使用以下代码解决。

#Force from http to https 
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]