我对这个问题的理解是重定向
http://static.photography.example.com/js/jquery.js
从以下地址获取文件(不更改浏览器上的 URL):
http://photography.example.com/js/jquery.js
但是将所有现有文件的 URL 保留为NOT REDIRECTED:
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]
此外,如果您有复杂的规则,您还可以使用MAPPING ,例如:
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]
map.txt
在文件夹中创建一个文本文档(例如).htaccess
并放入以下内容:
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]