1

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

6 Comments

This is good due to it's lower cpu usage, but can be a hassle to mantain, specially when adding servers.
@johnco: Thats what sh/phing scripts are for :-)
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 ;)
Strange, the PHP back tick didnt come through on that comment. That should surround the command there I listed.
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 - php.net/manual/en/function.symlink.php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.