views:

107

answers:

2

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

A: 

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

Brian Clozel
The CNAME technique didn't seem to work, see edit 2 of my question
ILMV
Did you:1- remove the previous .htaccess entry (Edit 1)2- add your static domain as a ServerAlias in your server configuration?
Brian Clozel
A: 

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]

LVS