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

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Multiple directory rerouting using rewrites


    I'm trying to rewrite multiple directories for my app using .htaccess

    All requests are processed via the 'app.php' file in my 'Web' directory which works absolutely fine but I've got my assets (CSS/JS/Images) in the 'Assets' folder but I want to be able to access them by pointing to 'css/filename.css'. The 'Assets' folder is outside the 'Web' directory

    If I try adding a anything to the root .htaccess telling the server rewrite requests with a '.css' or '.js' extension to the appropriate location in the 'Assets' folder, then the rewrite that sends all other requests to the 'app.php' file in the 'Web' directory stops working altogether.

    Requests:
    Code:
    http://localhost/page -> /htdocs/Web/app.php
    http://localhost/css/filename.css -> /htdocs/Assets/CSS/filename.css
    http://localhost/js/filename.js -> /htdocs/Assets/JS/filename.js
    Structure:
    Code:
    htdocs
    - .htaccess
    
    - Assets
    -- CSS
    --- file.css
    -- Images
    -- JS
    
    - Web
    -- .htaccess
    -- app.php
    /htdocs/.htaccess:
    Code:
    <IfModule mod_rewrite.c>
        #CSS
        RewriteCond %{REQUEST_URI} \.(css)$
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^css/(.*) Assets/CSS/$1 [L]
    
        RewriteRule .? - [S=2]
    
        #Reroute to app
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^$ Web/ [L]
        RewriteRule (.*) Web/$1 [L]
    </IfModule>
    /htdocs/Web/.htaccess
    Code:
    <IfModule mod_rewrite.c>
        DirectoryIndex app.php
    
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_URI} !\.(css)$
        RewriteRule ^(.*)$ app.php?$1 [PT,L]
    </IfModule>
    Thank you very much for your help!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Code:
    RewriteRule .? - [S=2]
    What's that doing there?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    That's in there to say skip the other rewrite conditions if it's a css file that's being requested

    Not allowed to post links as I'm a new user but look at the example for the S flag on the Apache "RewriteRule Flags" page
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    Is it safe to assume that doing this won't be possible?
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Originally Posted by FCM
    That's in there to say skip the other rewrite conditions if it's a css file that's being requested
    Not really. It literally says "if the requested file has anything in its name (which 99% of the time it will) then skip the next two rules".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    ok, i've removed that line but now all my requests are going to the htdocs/web directory so my CSS request is going to htdocs/Web/Assets/CSS rather than htdocs/Assets/CSS

IMN logo majestic logo threadwatch logo seochat tools logo