简体   繁体   English

使用.htaccess将wordpress上载文件夹重定向到s3存储桶

[英]Redirect wordpress uploads folder to s3 bucket using .htaccess

I'm trying to redirect my uploads folder to my s3 bucket using htaccess in wordpress however it's not working. 我正在尝试在WordPress中使用htaccess将上载文件夹重定向到s3存储桶,但无法正常工作。 All the images on my site are still getting served locally from the server instead of linking to the bucket. 我站点上的所有图像仍从服务器本地提供,而不是链接到存储桶。

This is my htaccess file: 这是我的htaccess文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteEngine On
RewriteRule ^wp-content/uploads/(.*)$ https://BUCKET.s3.amazonaws.com/wp-content/uploads/$1 [L]

# BEGIN W3TC CDN
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css)$">
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
# END W3TC CDN

There are a couple of problems. 有几个问题。 You need to put the rule ABOVE your wordpress rule because wordpress rules route every request to index.php. 您需要将规则放在wordpress规则之上,因为wordpress规则会将每个请求路由到index.php。 Next you will need to use mod_proxy with the use of the [P] flag because regardless of not specifying redirect in the rewriterule, Apache will do it anyway because the substitution URL is a new domain. 接下来,您将需要使用带有[P] flag mod_proxy ,因为无论是否未在重写器中指定重定向,Apache都会这样做,因为替换URL是一个新域。 So Apache will perform a Redirect. 因此,Apache将执行重定向。 So using [P] flag should proxy the content instead of doing a redirect. 因此,使用[P] flag应该代理内容而不是进行重定向。 This should accomplish what you need. 这应该可以满足您的需求。

RewriteEngine On
RewriteRule ^wp-content/uploads/(.*)$ https://BUCKET.s3.amazonaws.com/wp-content/uploads/$1 [P]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

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

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