Performance 使用.htaccess通过子域提供静态文件
好的,请原谅我的愚蠢,我已经在互联网上浏览了大量的例子,但我认为我没有找到我想要的 我有一个网站,Performance 使用.htaccess通过子域提供静态文件,performance,.htaccess,caching,cdn,static-content,Performance,.htaccess,Caching,Cdn,Static Content,好的,请原谅我的愚蠢,我已经在互联网上浏览了大量的例子,但我认为我没有找到我想要的 我有一个网站,photography.example.com是主网站,但我还想有另一个子域名来提供静态文件,例如static.photography.example.com 如果我请求一个文件(例如http://static.photography.example.com/js/jquery.js)我希望从非静态域检索该文件,使我能够保持文件结构完全不变,但使用多个域允许更多并发http请求 我不想抛出任何htt
photography.example.com
是主网站,但我还想有另一个子域名来提供静态文件,例如static.photography.example.com
如果我请求一个文件(例如http://static.photography.example.com/js/jquery.js
)我希望从非静态域检索该文件,使我能够保持文件结构完全不变,但使用多个域允许更多并发http请求
我不想抛出任何http响应,使浏览器认为文件已被移动,我只想将文件从普通域镜像到静态域。在此之后,我将继续设置far future expired以改进缓存等
如何使用.htaccess
实现这一点
编辑1
所以经过一段时间的胡闹之后,我想到了这个:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://photography.example.com/$1 [L]
但这实际上会重定向到我正在尝试读取的域,我希望它以静态域名提供文件,任何修改此脚本的帮助都将不胜感激
编辑2
所以我修改了我的DNS,等了几天它传播,但是CNAME技术也不起作用。以下是条目:
在你的问题中,你说的是:
对于第#1项,您可以在主域上编辑httpd.conf/.htaccess文件(在整个网站上编辑不会有什么坏处,不是吗?)
过期于
ExpiresByType应用程序/x-javascript A2592000
ExpiresByType图像/gif A2592000
过期按类型图像/jpeg A2592000
ExpiresByType图像/png A2592000
第2项不需要任何Apache配置-只需使用CNAME photography.example.com配置static.photography.example.com DNS条目即可。这应该能奏效
或者,您可以编辑httpd.conf并添加服务器别名
<VirtualHost xx.xxx.xxx.xx:80>
DocumentRoot /
ServerName photography.example.com
ServerAlias static.photography.example.com
</VirtualHost>
DocumentRoot/
ServerName.example.com
ServerAlias static.photography.example.com
所以现在,您不需要一个具有专用Apache配置的独立虚拟主机。
以下是希望拥有一个独立域和一个具有专用配置的独立虚拟主机的其他几个原因:
http://static.photography.example.com/images.jpg
- 你的主域有cookies,你想
- 您(或您的Amazon S3帐户,以减少主主机上的带宽)
过期于
ExpiresByType应用程序/x-javascript A2592000
ExpiresByType图像/gif A2592000
过期按类型图像/jpeg A2592000
ExpiresByType图像/png A2592000
第2项不需要任何Apache配置-只需使用CNAME photography.example.com配置static.photography.example.com DNS条目即可。这应该能奏效
或者,您可以编辑httpd.conf并添加服务器别名
<VirtualHost xx.xxx.xxx.xx:80>
DocumentRoot /
ServerName photography.example.com
ServerAlias static.photography.example.com
</VirtualHost>
DocumentRoot/
ServerName.example.com
ServerAlias static.photography.example.com
所以现在,您不需要一个具有专用Apache配置的独立虚拟主机。
以下是希望拥有一个独立域和一个具有专用配置的独立虚拟主机的其他几个原因:
http://static.photography.example.com/images.jpg
- 你的主域有cookies,你想
- 您(或您的Amazon S3帐户,以减少主主机上的带宽)
如果您想要其中一个,或者如果您的缓存需求太复杂(您不想缓存所有JS/CSS/图像,而是缓存其中的一个子集),然后,您唯一的解决方案是:使用httpd.conf并为每个域编写单独的配置我认为您需要CNAME记录将static.photography.example.com的请求传递到服务器,并以特殊方式对static.photography.example.com的have.htaccess解析请求 将以下重写规则添加到.htaccess应该可以做到这一点
RewriteCond%{HTTP\u HOST}^(www\)?photography.example.com$[NC]
我认为您需要CNAME记录将对static.photography.example.com的请求传递到您的服务器,并以特殊方式对static.photography.example.com的have.htaccess解析请求
将以下重写规则添加到.htaccess应该可以做到这一点
RewriteCond%{HTTP\u HOST}^(www\)?photography.example.com$[NC]
我对这个问题的理解是重定向
http://static.photography.example.com/js/jquery.js
要从以下地址获取文件(不更改浏览器上的URL):
但保留指向所有现有文件的URL,如以下文件,以不重定向:
http://static.photography.example.com/images.jpg
如果为真,则此.htaccess
应能工作:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?.photography.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://photography.example.com/$1 [P]
此外,如果您有以下复杂规则,也可以使用映射:
RewriteRule ^/file1\.js$ http://photography.example.com/file.js?q=444 [P]
RewriteRule ^/file2\.js$ http://photography.example.com/file.js?q=345 [P]
RewriteRule ^/file3\.js$ http://photography.example.com/file.js?q=999 [P]
在带有.htaccess
的文件夹中创建文本文档(例如map.txt
),并放入以下内容:
file1 444
file2 345
file3 999
.htaccess
将具有以下外观:
# Set a variable ("map") to access map.txt from config
RewriteMap map txt:map.txt
# Use tolower function to convert string to lowercase
RewriteMap lower int:tolower
# Get requested file name
RewriteCond %{REQUEST_URI} ^/([^/.]+)\.js$ [NC]
# Seek file name in map-file
RewriteCond ${map:${lower:%1}|NOT_FOUND} !NOT_FOUND
# Perform rewriting if the record was found in map-file
RewriteRule .? http://photography.example.com/file.js?q=${map:${lower:%1}} [P]
我对这个问题的理解是重定向
http://static.photography.example.com/js/jquery.js
要从以下地址获取文件(不更改浏览器上的URL):
但保留指向所有现有文件的URL,如以下文件,以不重定向:
http://static.photography.example.com/images.jpg
如果为真,则此.htaccess
应能工作:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?.photography.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://photography.example.com/$1 [P]
此外,如果需要,还可以使用映射