Ben Everard
Ben Everard

Reputation: 13804

Using .htaccess to serve static files via a subdomain

Right, excuse my stupidity, I've looked through a load of examples on t'interweb but I don't think I've found what I'm looking for.

I have a website, photography.example.com is the main site but I also want to have another subdomain to serve static files, for example static.photography.example.com.

If I request a file (e.g. http://static.photography.example.com/js/jquery.js) I want that file to be retrieved from the non-static domain, allowing me to keep my file structure completely untouched but using multiple domains to allow more concurrent http requests.

I don't want to throw any http responses that would make the browser thing the file has been moved, I just want to mirror the files from the normal domain to the static domain. After this I would proceed to set far future expired to improve caching etc.

How do I achieve this using .htaccess?

EDIT 1

So after a bit of messing around I have come up with this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://photography.example.com/$1 [L]

But this actually redirects to the domain I'm trying to read, I want it to serve the file up under the static domain name, any help with modifying this script would be greatly appreciated.

EDIT 2

So I've amended my DNS and waited a few days for it to propagate but the CNAME technique doesn't work either. Here's the entry:

alt text

Upvotes: 0

Views: 4230

Answers (3)

eQ19
eQ19

Reputation: 10701

My understanding to the question is to REDIRECT

http://static.photography.example.com/js/jquery.js

to get the file from following address (without changing the URL on the browser):

http://photography.example.com/js/jquery.js

But keep URL to all existing files like the following to NOT REDIRECTED:

http://static.photography.example.com/images.jpg

If it is true then this .htaccess should work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?.photography.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://photography.example.com/$1 [P]

Moreover you can also use MAPPING if you have a complex rules like:

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]

create a text document (e.g. map.txt) in the folder with .htaccess and put in the following:

file1   444
file2   345
file3   999

The .htaccess will have the following look:

# 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]

Upvotes: 1

LVS
LVS

Reputation: 571

I think you would need the CNAME record to pass the request for static.photography.example.com to your server and the have .htaccess parse requests for static.photography.example.com in a special manner.

Adding the following rewrite rule to .htaccess should do the trick

RewriteCond %{HTTP_HOST} !^(www\.)?photography.example.com$ [NC]

Upvotes: 0

Brian Clozel
Brian Clozel

Reputation: 59191

In your question, you're talking about:

  1. Adding Expire headers for caching
  2. Splitting resources across domains

For Item #1, you can edit your httpd.conf/.htaccess file on your main domain (it doesn't hurt doing it on your whole website, no?)

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType application/x-javascript A2592000
  ExpiresByType image/gif A2592000
  ExpiresByType image/jpeg A2592000
  ExpiresByType image/png A2592000
</IfModule> 

Item #2 doesn't need any Apache configuration - just configure your static.photography.example.com DNS entry with CNAME photography.example.com. That should do the trick.

Or you can edit your httpd.conf and add a ServerAlias

<VirtualHost xx.xxx.xxx.xx:80> 
DocumentRoot / 
ServerName photography.example.com 
ServerAlias static.photography.example.com 
</VirtualHost> 

So for now, you don't need a separate virtual host with dedicated Apache configuration.

Here are a few other reasons why you'd want a separate domain, and a separate virtual host with dedicated configuration:

If you want one of those, or if your caching needs are too complex (you don't want to cache all JS/CSS/images, but rather a subset of it), then your only solution is: get your hands on your httpd.conf and write separate configurations for each domain

Upvotes: 2

Related Questions