Apache .htaccess-使用https和trailing/in-one-step进行重写会产生意外的后果

Apache .htaccess-使用https和trailing/in-one-step进行重写会产生意外的后果,apache,.htaccess,mod-rewrite,redirect,https,Apache,.htaccess,Mod Rewrite,Redirect,Https,我最近使用cloudflare向我的网站添加了ssl证书。我在.htaccess中添加了一些行,以默认使用https。查看google page speed insights,我注意到当使用http访问我网站的子目录时,发生了三次重定向。例如,访问时会出现以下重定向链 https:://markfisher.photo/galleries(很抱歉,没有足够的代表来链接>2个链接。双冒号用于断开链接) http://markfisher.photo/galleries/ https:://mar

我最近使用cloudflare向我的网站添加了ssl证书。我在.htaccess中添加了一些行,以默认使用https。查看google page speed insights,我注意到当使用http访问我网站的子目录时,发生了三次重定向。例如,访问时会出现以下重定向链

  • https:://markfisher.photo/galleries(很抱歉,没有足够的代表来链接>2个链接。双冒号用于断开链接)
  • http://markfisher.photo/galleries/
  • https:://markfisher.photo/galleries/
  • 我通过修改.htaccess中的相关行修复了这一问题(即一步将https和尾部斜杠组合在一起)

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTP_HOST} ^markfisher.photo$ [OR]
    RewriteCond %{HTTP_HOST} ^www.markfisher.photo$
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}\/ [L,R]
    
    与以前相比,唯一的变化是增加/接近底线的末尾。这与我的网站的子目录一样有效,但是当使用http://markfisher.photo访问根目录时,它会被重定向到https://markfisher.photo//。这个很好用,但看起来很难看。我不知道如何阻止双斜杠的发生。我猜它与我的主机使用的一个全局.htaccess文件有关,但我不知道该文件可能包含什么,所以我正在努力解决这个问题


    另外,如果有人能告诉我如何在没有任何额外重定向的情况下让www进入那里,那将是非常棒的。

    下面的方法奏效了。我分为两种情况——一种情况是尾部斜杠不存在,需要重写,另一种情况是它存在,因此不需要额外的斜杠

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTP_HOST} ^markfisher.photo$ [OR]
    RewriteCond %{HTTP_HOST} ^www.markfisher.photo$
    RewriteCond %{REQUEST_URI} !^/$
    RewriteCond %{REQUEST_URI} !^/.*/$
    RewriteRule ^ https://www.markfisher.photo%{REQUEST_URI}\/ [R=301]
    
    
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTP_HOST} ^markfisher.photo$ [OR]
    RewriteCond %{HTTP_HOST} ^www.markfisher.photo$
    RewriteCond %{REQUEST_URI} ^.*/$
    RewriteRule ^ https://www.markfisher.photo%{REQUEST_URI} [R=301]
    

    根文件夹的请求URI为
    /
    ,然后添加一个额外的
    /
    。如果这些目录是通过HTTP请求的,并且已经附加了一个尾随斜杠,那么你的目录也会发生这种情况。那么是否有一个重写条件,我可以用来只针对根文件夹,并为此应用重写规则。?如果这是最好的方法?