Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services Cloudfront未使用尾部斜杠重定向_Amazon Web Services_Amazon S3_Amazon Cloudfront - Fatal编程技术网

Amazon web services Cloudfront未使用尾部斜杠重定向

Amazon web services Cloudfront未使用尾部斜杠重定向,amazon-web-services,amazon-s3,amazon-cloudfront,Amazon Web Services,Amazon S3,Amazon Cloudfront,我正在通过Cloudfront托管一个包含多个子域的网站。 当我转到www.domain.com/subdomain/(注意:带尾随斜杠)时,网站将正确加载并从www.domain.com/subdomain/***.js获取缩小的.js和.css文件 但是,如果我导航到www.domain.com/subdomain而不带尾随斜杠,站点的index.html仍会被提供,但资产试图从www.domain.com/**.js获取 我试着使用lamba@edge函数来更改请求uri并附加斜杠,但这不

我正在通过Cloudfront托管一个包含多个子域的网站。 当我转到
www.domain.com/subdomain/
(注意:带尾随斜杠)时,网站将正确加载并从
www.domain.com/subdomain/***.js
获取缩小的.js和.css文件

但是,如果我导航到
www.domain.com/subdomain
而不带尾随斜杠,站点的index.html仍会被提供,但资产试图从
www.domain.com/**.js
获取


我试着使用lamba@edge函数来更改请求uri并附加斜杠,但这不起作用。谢谢你的帮助

这将由html引用相对文件路径引起(即
src=“**.js”
vs
src=“/subdomain/**.js”

如果要修复此问题,则需要在用户浏览器中执行重定向到斜杠路径。这可以通过使用Lambda@Edge函数在
原点响应
事件中执行重定向

下面是一个函数

 def lambda_handler(event, context):
 
     # Generate HTTP redirect response with 302 status code and Location header.
     
     response = {
         'status': '302',
         'statusDescription': 'Found',
         'headers': {
             'location': [{
                 'key': 'Location',
                 'value': 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html'
             }]
         }
     }
     
     return response
为此,您需要添加自定义逻辑,通过检查请求的最后一个字符是否为“/”字符来检查URL是否需要重定向


此外,如果您可以将css、js和图像的路径从相对路径更改为绝对路径,如本答案顶部所述。

这将由html引用相对文件路径(即
src=“**.js”
vs
src=“/subdomain/**.js”
)引起

如果要修复此问题,则需要在用户浏览器中执行重定向到斜杠路径。这可以通过使用Lambda@Edge函数在
原点响应
事件中执行重定向

下面是一个函数

 def lambda_handler(event, context):
 
     # Generate HTTP redirect response with 302 status code and Location header.
     
     response = {
         'status': '302',
         'statusDescription': 'Found',
         'headers': {
             'location': [{
                 'key': 'Location',
                 'value': 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html'
             }]
         }
     }
     
     return response
为此,您需要添加自定义逻辑,通过检查请求的最后一个字符是否为“/”字符来检查URL是否需要重定向

此外,如果您可以将css、js和图像的路径从相对更改为绝对,如本答案顶部所述