.htaccess - 用https重写和尾随/一步都有意想不到的后果

时间:2021-09-08 15:20:10

I added an ssl certificate to my website recently using cloudflare. I added some lines to my .htaccess to use https by default. Looking at google page speed insights, I notice that when visiting a sub-directory of my website using http, three redirects were occurring. For example the following redirect chain would occur when visiting http://markfisher.photo/galleries.

我最近使用cloudflare在我的网站上添加了一个ssl证书。我在.htaccess中添加了一些行,默认情况下使用https。查看谷歌页面速度洞察,我注意到当使用http访问我的网站的子目录时,发生了三次重定向。例如,访问http://markfisher.photo/galleries时会出现以下重定向链。

  1. http://markfisher.photo/galleries
  2. https:://markfisher.photo/galleries (Sorry, not enough rep for >2 links. Double colon is to break link)
  3. https :: //markfisher.photo/galleries(对不起,没有足够的代表> 2个链接。双冒号是打破链接)

  4. http:://markfisher.photo/galleries/
  5. https:://markfisher.photo/galleries/

I fixed this (i.e. combining the https and trailing slash in one step) by modifying the relevant lines in .htaccess to

我通过修改.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]

The only change from before was adding / towards the end of the bottom line. This works as intended for subdirectories of my website, but when visiting the root directory with http http:://markfisher.photo, it gets redirected to https:://markfisher.photo//. This works fine but looks ugly. I can't work out how to stop the double trailing slashes from occuring. I'm guessing it has something to do with a global .htaccess file used by my host but I don't know what that file(s) might contain so I'm struggling to work it out.

之前唯一的变化是增加/接近底线。这适用于我的网站的子目录,但是当使用http http://markfisher.photo访问根目录时,它会被重定向到https :: //markfisher.photo//。这很好但看起来很难看。我无法弄清楚如何阻止双尾随斜杠的发生。我猜它与我的主机使用的全局.htaccess文件有关,但我不知道那个文件可能包含什么,所以我很难解决这个问题。

Also if anyone could tell my how to get www in there as will without any additional redirects that would be great.

此外,如果有人可以告诉我如何在那里获得www,没有任何额外的重定向,这将是伟大的。

1 个解决方案

#1


The following worked. I separated into 2 cases - one where the trailing slash was absent and needed to be rewritten in, and on where it was present and so an extra slash was unwanted.

以下工作。我把它分成2个案例 - 一个没有尾随斜线但需要重写的案例,以及它存在的地方,因此不需要额外的斜线。

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]

#1


The following worked. I separated into 2 cases - one where the trailing slash was absent and needed to be rewritten in, and on where it was present and so an extra slash was unwanted.

以下工作。我把它分成2个案例 - 一个没有尾随斜线但需要重写的案例,以及它存在的地方,因此不需要额外的斜线。

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]