Search code examples
wordpress.htaccessamazon-s3amazon-cloudfront

wordpress wp-content/uploads folder 301 redirect to S3 bucket by cloudfrond CDN by .htaccess issues


See below, the .htacess file from Wordpress. If I remove the part starting from # BEGIN WordPress, all the image file URLs will be changed to d28rt1vkpsdxas.cloudfront.net/someimage.png successfully.

However, all the internal links will be unable to access (only the home page can access). The error is

Not Found The requested URL /shop/ was not found on this server.

But if I keep the part start from # BEGIN WordPress, the CDN redirect will not work anymore. All photos are missing.

Options +FollowSymlinks

RewriteEngine on

Rewriterule ^wp-content/uploads/(.*)$ http://exampled28t1vkps.cloudfront.net/$1 [r=301,nc]

# 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

Solution

  • You’re missing the [L] flag, which prevents other rules from redirecting:

    Rewriterule ^wp-content/uploads/(.*)$ http://exampled28t1vkps.cloudfront.net/$1 [r=301,nc,l]