需要帮助为HTTPS正确配置.htaccess

时间:2019-04-11 07:32:39

标签: .htaccess

我正在尝试正确配置和写入.htaccess文件。我已经尝试了一些方法,但是不知何故它们都无法解决。

我正在尝试完成以下重定向:

  • 所有文件夹(包括子文件夹)从HTTP重定向到HTTPS
  • 删除“ .html”结尾,以便该网址显示www.domain.com/photos而不是www.domain.com/photos.html
  • 在主页上删除“ index.html”,因此它仅显示www.domain.com

有些事情可行,但是当进入子目录(如www.domain.com/sub)时,页面将从HTTPS更改为HTTP。

此外,当通过HTTPS访问页面时,它没有正确加载HTML文件中指定的字体 ()

请参见下面的代码:

            <IfModule mod_rewrite.c>
            AddType text/x-component .htc
             RewriteEngine On
             RewriteBase /
             RewriteCond %{HTTPS} !=on
             # This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
             # This rule will redirect users from their original location, to the same location but using HTTPS. # i.e. http://www.example.com/foo/ to https://www.example.com/foo/ # The leading slash is made optional so that this will work either in httpd.conf or .htaccess context
             # remove .html; use THE_REQUEST to prevent infinite loops
             RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
             RewriteRule (.*)\.html$ $1 [R=301]
             # remove index
             RewriteRule (.*)/index$ $1/ [R=301]
             # remove slash if not directory
             RewriteCond %{REQUEST_FILENAME} !-d
             RewriteCond %{REQUEST_URI} /$
             RewriteRule (.*)/ $1 [R=301]
             # add .html to access file, but don't redirect
             RewriteCond %{REQUEST_FILENAME}.html -f
             RewriteCond %{REQUEST_URI} !/$
             RewriteRule (.*) $1\.html [L]
            </IfModule>

1 个答案:

答案 0 :(得分:0)

我想通了,到目前为止,它仍然有效:

<IfModule mod_rewrite.c>
AddType text/x-component .htc
 RewriteEngine On
 RewriteBase /
 # Redirect to www 
 RewriteCond %{HTTP_HOST} !^www\.domain\.ch [NC,OR]
 # Redirect to https
 RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
 RewriteRule ^ https://www.domain.ch%{REQUEST_URI} [NE,R=301,L]
 # remove .html; use THE_REQUEST to prevent infinite loops
 RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
 RewriteRule (.*)\.html$ $1 [R=301]
 # remove index
 RewriteRule (.*)/index$ $1/ [R=301]
 # remove slash if not directory
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} /$
 RewriteRule (.*)/ $1 [R=301]
 # add .html to access file, but don't redirect
 RewriteCond %{REQUEST_FILENAME}.html -f
 RewriteCond %{REQUEST_URI} !/$
 RewriteRule (.*) $1\.html [L]
</IfModule>