views:

162

answers:

2

My root already has all the css and image files I require

  • www.example.com (my domain)
  • www.example.com/css/style.css (my styles)

On my hosting, my sub domain has to live inside a folder

root folder 
  public_html/
    index.php
    css/
      style.php
  subdomains/
      admin/
        .htaccess
        index.php

How can I use mod_rewrite in my subdomain to redirect requests for css into my root folder?

FINAL SOLUTION

Thanks! I used Shikhar's answer and modified it to also work with images and js.

  RewriteCond %{HTTP_HOST} admin.example.com
  RewriteCond %{REQUEST_URI} [css|images|js]/(.*)\.(.*)
  RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,NC,L]
+2  A: 

If you have access to the box, why not just create a symbolic link from the subdomain folder to the root folder. You should be able to do this through your file manager or though a shell prompt on the box.

Mech Software
This is good due to it's lower cpu usage, but can be a hassle to mantain, specially when adding servers.
Johnco
@johnco: Thats what sh/phing scripts are for :-)
prodigitalson
Doesnt mention if this is going to be deployed out to a lot of servers or not. It could however be done in a script on the box as part of adding the subdomain. You can even do it in PHP as part of the setup with `ln ../public_html/css` right in the PHP code. Might want to actually check to see if the directory exists though before calling that a bazillion times ;)
Mech Software
Strange, the PHP back tick didnt come through on that comment. That should surround the command there I listed.
Mech Software
Unfortunately this is on some cheaper hosting and so I don't have access. Which is why I am forced to have my subdomains in a seperate directory in the first place. Thanks for your discussion anyway!
Jon Winstanley
You should be able to create a symbolic link via PHP. I created a very simple PHP script to solve exactly the same problem as you on my last shared hosting server (which otherwise didn't have access to do it). See the symlink() function - http://php.net/manual/en/function.symlink.php
Paolo
+1  A: 

RewriteCond %{HTTP_HOST} xyz.example.com

RewriteCond %{REQUEST_URI} css/style.css

RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,NC,L]

This requires redirect though.

shikhar