6

I'm trying to serve CSS files for my site from a separate domain.

I've got the following entry in my site's .htaccess (in the document root):

RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]

With the intention of simply matching:

http://www.mysite.com/templates/default/styles/main.css

… and sending an HTTP 301 redirect to:

http://css.mysite.com/templates/default/styles/main.css

But what's actually getting called is:

http://css.mysite.com/templates/.css

(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)

Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
gellenburg
  • 63
  • 3
  • 1
    Just to make sure, you don't have a way just to *request* the css directly from the other URL, right? That is, change the pages to make the request directly instead of doing a redirect? – Andrew Feb 15 '12 at 00:23
  • I'm trying to keep the site as portable as possible. I realize I'll be taking a hit with the 301 redirect, but by keeping things in the .htaccess file, if I ever need to move the site to a new domain or servers for disaster recovery purposes simply removing the lines from the .htaccess file would revert everything back to being served locally. At least, that's what my thought is. :-) This way I can override the CSS without editing any of the CSS files internally. The site's going to be locked down tight when it goes live. – gellenburg Feb 15 '12 at 00:27

1 Answers1

7

Try this and let me know:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]

or a more "dynamic way"

RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]

This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.

or be even more generic:

RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]

You redirecting everything that start with templates

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • Interesting. I guess I could create jpg.mysite.com, css.mysite.com, js.mysite.com, png.mysite.com, and gif.mysite.com. Talk about parallelizing http connections! :-) I think that would be too much trouble though. I did try your first example though and it seems that everything under "templates" is being redirected to the subdomain (not just the css extension). :/ – gellenburg Feb 15 '12 at 00:57
  • I agree, you can create a "media" like: media.mysite.com and redirect everything there also - I updated my answer – Book Of Zeus Feb 15 '12 at 01:11
  • @gellenburg, it seems ***very odd*** that everything is redirecting to `css.mysite.com` if you've typed in BOZ's example 1 correctly. This rule will only fire for `GET /templates/*.css`. Use the regexpChecker.php and note the 301 traps on this thread: [Tips for debugging .htaccess rewrite rules](http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules) – TerryE Feb 15 '12 at 01:15
  • The problem was the CSS was using relative URLs. @Book of Zeus thank you very much. That solution worked very well! – gellenburg Feb 15 '12 at 01:54