简体   繁体   English

删除.htaccess中的结尾斜杠以供CloudFront使用

[英]Remove trailing slash in .htaccess for CloudFront use

There are plenty of 'remove trailing slash' bits of regex here on StackOverflow, and they're all lovely, but they all have the same issue: they don't work too well with a CDN service like Amazon CloudFront. StackOverflow上有很多正则表达式的“删除尾部斜杠”,它们都很可爱,但是它们都有相同的问题:它们不能与Amazon CloudFront等CDN服务一起使用。

So, my setup is that www.example.com is Amazon Cloudfront, while my real website is, say, on real.example.com. 因此,我的设置是www.example.com是Amazon Cloudfront,而我的真实网站是在real.example.com上。

All the three below will happily remove trailing slashes from www.example.com/directory/ but will rewrite the URL as real.example.com/directory which clearly I don't want. 下面的所有三个将愉快地从www.example.com/directory/删除结尾的斜杠,但会将URL重写为real.example.com/directory ,这显然是我所不希望的。

I've used 我用过

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ %{REQUEST_URI} [R=301,L]

or 要么

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

or 要么

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]

...but all of these will expose the 'real' domain name. ...但是所有这些都会公开“真实”域名。

How can I write a regex rewrite rule that will correctly remove the trailing slash but not rewrite the domain to the origin website? 如何编写正则表达式重写规则,该规则将正确删除尾部斜杠但不将域重写到原始网站?

(I don't mind hard-coding the domain URL in here, but I need to keep the http or https bit intact.) (我不介意在此处对域URL进行硬编码,但我需要保持http或https位不变。)

Did you try something like this? 你尝试过这样的事情吗?

# Set "protossl" to "s" if we were accessed via https://. This is used later
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http%{ENV:protossl}://www.example.com/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM