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

时间:2022-11-23 13:26:02

I'm trying to redirect my uploads folder to my s3 bucket using htaccess in wordpress however it's not working. All the images on my site are still getting served locally from the server instead of linking to the bucket.

我正在尝试使用wordpress中的htaccess将我的uploads文件夹重定向到我的s3存储桶但是它无效。我的网站上的所有图像仍然从服务器本地提供,而不是链接到存储桶。

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

1 个解决方案

#1


1  

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. 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. So Apache will perform a Redirect. So using [P] flag should proxy the content instead of doing a redirect. This should accomplish what you need.

有几个问题。你需要将规则放在你的wordpress规则之上,因为wordpress规则将每个请求都路由到index.php。接下来,您将需要使用mod_proxy并使用[P]标志,因为无论是否在重写中指定重定向,Apache都会这样做,因为替换URL是一个新域。所以Apache将执行重定向。因此,使用[P]标志应该代理内容而不是重定向。这应该可以满足您的需求。

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

#1


1  

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. 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. So Apache will perform a Redirect. So using [P] flag should proxy the content instead of doing a redirect. This should accomplish what you need.

有几个问题。你需要将规则放在你的wordpress规则之上,因为wordpress规则将每个请求都路由到index.php。接下来,您将需要使用mod_proxy并使用[P]标志,因为无论是否在重写中指定重定向,Apache都会这样做,因为替换URL是一个新域。所以Apache将执行重定向。因此,使用[P]标志应该代理内容而不是重定向。这应该可以满足您的需求。

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