#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Too simple? Not for me!


    I apologize if this is the wrong place or too elementary, but...

    I have multiple subdomains at BlueHost, all one level down for public_html, that provide identical services for different customers.

    To save myself endless replication of identical javascript and css files, I would like statements of the form

    <link href="scripts/override.css" rel="stylesheet" type="text/css">

    to be redirected up-account to (say) "root/myresources/scripts/override.css"

    There's clearly some way to do this with 'mod_rewrite' or 'alias' of 'follow symlinks' - OR SOMETHING! - but I'm too much of a script kiddie in this area, there are too many ways to go wrong, and I can't find a simple example (probably because it's blindingly obvious to most users!)
    (Oh. and the proposal to use ../ to go levels does not work.)

    So please,

    1. What directive would I write to achieve my purpose?
    2. What file would I put that directive in so that it would get executed?

    Thanks for any help you could give.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    In your shoes I would go for symlinks first. Very easy to manage, and the filesystem setup is very logical. Make sure you have Options +FollowSymLinks for your site (like in the .htaccess) and symlink the site's scripts/ to that place under root/.

    If that's not possible then Alias, but that's only available in the server config and virtual host. Probably can't do that.

    Otherwise a simple RewriteRule can do it. Along with the RewriteEngine,
    Code:
    RewriteRule ^scripts(.*) /path/to/root/myresources/scripts$1 [L]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    It's a start


    Thanks. Your example sent me back to the drawing board. It does not achieve what I want it to because it will not (at least in my tests) let me redirect things higher up than the document root.

    What I really need is an Alias directive, but that's not available in .htaccess.

    I had previously used a trick as follows:
    <link href="index.php?=someValue"> and then in index.php jumped up a couple of levels and used the 'someValue' to push resources out.

    I believe (have not tested 'cause it's late) that this rewrite rule will let me move that trick out of sight:

    If I have <link href="myfile.css">, then a rewrite rule that says

    RewriteRule (.*).css index.php?theFile=$1.css

    will give index .pdp enough info to deliver the desired file.

    We'll see.
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4303
    Welcome to DevShed Forums, jrisken.

    Given those limitations, I'd do that myself if I didn't create a separate subdomain for the stylesheets and other resources (which can have certain advantages, like avoiding the overhead of cookies).

    One note of caution though. You may want to put some validation in place so that people can't use the script to view the contents of all of your PHP files.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    And the best answer is...


    Thanks, everybody, for the suggestions. Here are two excellent solutions.
    Problem: I want resources such as images and scripts NOT stored in (or under) the document root for a domain. In my case this is because I have multiple domains that all require the same resources. So in the environment of my shared server on BlueHost, a typical site's document root might be at
    /home/myacct/public_html/mysite
    and if I wanted to get a CSS file I would say
    <link href="mycss.css"> and that would require my CSS file to be in the mysite folder. And I'd have to have the identical file in mysite2, mysite3, etc.
    What I'd really like to be able to do is store all such resouces in
    /home/myacct/myresources.

    What people suggested and I tried and found would not work
    a) Use double-dot notation to go above the document root
    <link href="../mycss.css">
    b) Use mod_rewrite in .htaccess
    options +SymlinkIfOwnerMatch
    RewriteRule mycss.css /home/myacct/myresources/mycss.css
    c) Use Alias in .htaccess (available only in httpd.conf - at the server level)

    What does work. There are two solutions. For the sake of simplicity, let's assume that I'm going APPEAR to locate all my resources in a subdirectory of the document root. So my CSS link will look like this:
    <link href="resources/mycss.css">
    But in fact my resources will be in /home/myacct/myresources

    Solution 1: Use ssh and the linux symbolic link command to create a fake subdirectory in the document root of each of your sites
    cd /home/myacct/public_html/mysite
    ln -s /home/myacct/myresources resources

    Solution 2: Use the PHP symlink command to achieve the same effect:
    <?php @symlink("/home/myacct/myresources", "home/myacct/public_html/mysite/resources"); ?>
    If you run this command as part of your regular scripting, it will create the link but then halt processing. Running hte script a second time will throw a warning because the link has already been created, and the @ in front of the command gobbles that warning. As a matter of good practice you should not leave this command in your regular scirpt.

    Well, folks, I spent about 30 hours getting to this point. I hope this will save you at least some of that time.

IMN logo majestic logo threadwatch logo seochat tools logo