【问题标题】:Redirect to CDN重定向到 CDN
【发布时间】:2016-05-01 20:24:04
【问题描述】:

我在 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 的内容。

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

谢谢!

【问题讨论】:

  • 对不起,我会把我的回复写成代码格式化的答案。

标签: wordpress apache .htaccess redirect cdn


【解决方案1】:

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

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

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

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

【讨论】:

  • 我已经尝试了 100 种不同的组合,但到目前为止没有一个对我有用。该网站甚至从我目前拥有的 http 重定向到 https。
  • @naerak 是否有您使用的 CDN 论坛?也许他们会更有帮助。另外,我不建议使用上面发布的内容从 http 重定向到 https。我是说这就是我重定向的方式,也许您可​​以使用同一行代码在您的 apache 配置文件中重定向。我在您的虚拟主机中看到它,但它已被注释掉。
猜你喜欢
  • 2019-01-18
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2011-11-25
相关资源
最近更新 更多