重定向到CDN

时间:2016-05-01 20:24:05

标签: wordpress apache .htaccess redirect cdn

我在bitnami上安装了wordpress。我正在尝试为wp-content文件夹的内容设置CDN。 CDN将AWS Cloudfront与S3存储桶中的wp-content文件夹的所有内容结合使用。 我无法获得apache将任何资产请求重定向到CDN。我已经尝试了很多东西,但我无法让它发挥作用。

htaccess.conf文件的内容。

RewriteCond %{HTTP_HOST} ^(www\.)?site.com
RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
RewriteCond %{REQUEST_URI} ^(.*)\.png$
RewriteRule ^/(.*)$ https://site.cloudfront.net/$1 [NC,NE,R=301]

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
<FilesMatch "\.(js|css)$">
   ExpiresActive On
   ExpiresDefault "access plus 1 week"
</FilesMatch>
ExpiresDefault "access 1 month"
</IfModule>
# Expires Caching End #

<Directory "/opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/akismet">
# Only allow direct access to specific Web-available files.

# Apache 2.2
<IfModule !mod_authz_core.c>
    Order Deny,Allow
    Deny from all
</IfModule>

# Apache 2.4
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>

# Akismet CSS and JS
<FilesMatch "^(form|akismet)\.(css|js)$">
    <IfModule !mod_authz_core.c>
        Allow from all
    </IfModule>

    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
</FilesMatch>

# Akismet images
<FilesMatch "^(.+)\.(png|gif)$">
    <IfModule !mod_authz_core.c>
        Allow from all
    </IfModule>

    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
</FilesMatch>

# compression
<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

#</ifModule>
# BEGIN Compress text files
<ifModule mod_deflate.c>
<filesMatch "\.(css|js|x?html?|php)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
# END Compress text files
</Directory>

http-app.conf的内容

<VirtualHost *:80>
    ServerName site.com
    ServerAlias www.site.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
    # Added the following 3 lines to force https
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
    #RewriteRule ^wp-content/uploads/(.*)$ https://site.cloudfront.net/wp-content/uploads/$1 [P]
    #Redirect permanent /wp-content/ https:/site.cloudfront.net/wp-content/
    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:443>
    ServerName site.com
    ServerAlias www.site.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
    SSLEngine on
    SSLCertificateFile "/opt/bitnami/apps/wordpress/conf/certs/www.site.crt"
    SSLCertificateKeyFile "/opt/bitnami/apps/wordpress/conf/certs/www.site.key"
    SSLCACertificateFile "/opt/bitnami/apps/wordpress/conf/certs/www.site.crt"
    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

httpd-app.conf的内容

RewriteCond %{HTTP_HOST} ^(www\.)?site.com
RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
RewriteCond %{REQUEST_URI} ^(.*)\.png$
RewriteRule ^/(.*)$ https://site.cloudfront.net/$1 [NC,NE,R=301]

<IfDefine USE_PHP_FPM>
    <Proxy "unix:/opt/bitnami/php/var/run/wordpress.sock|fcgi://wordpress-fpm" timeout=300>
    </Proxy>
</IfDefine>

<Directory "/opt/bitnami/apps/wordpress/htdocs">
# Added +Includes for CDN testing. KA
    Options +MultiViews +FollowSymLinks +Includes
# Change from None to All. KA
    AllowOverride All
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>

    <IfDefine USE_PHP_FPM>
       <FilesMatch \.php$>
         SetHandler "proxy:fcgi://wordpress-fpm/"
       </FilesMatch>
    </IfDefine>


    RewriteEngine On
# Do not touch the lines below. Added for redierect to www. KA
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
</Directory>

Include "/opt/bitnami/apps/wordpress/conf/htaccess.conf"

如果有人注意到与重定向相关的任何冗余代码,那么我就会对此进行测试以使其正常工作。

我可以访问站点外部的S3存储桶和云端URL的内容,没有任何问题。

任何正确方向的解决方案或指针都会有所帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定这是否会对您有所帮助,但就重定向而言:

当我从我网站的http版本重定向到https时,我在apache2.conf文件中使用此行:

Redirect "/" "https://www.version.of.my.site"

我注意到你的http-app.conf中的重定向行被注释掉了。