Welcome Guest, Not a member yet? Register   Sign In
Multiple .htaccess
#1

[eluser]simshaun[/eluser]
What I'd like to do is implement caching through my htaccess file, but I only want it enabled for a subdirectory of controllers.

Normally, I could put a .htaccess file in a subdirectory if I wanted it only to apply for that directory. With CI, I'm a little befuddled about how to mimic this.

This is what ought to happen...
.htaccess in root contains rules for everything
.htaccess in controllers/admin/ contains rules only for files under http://localhost/admin/

I've tried this in the root .htaccess file:
<Directory /admin>
ExpiresActive on
ExpiresDefault "modification plus 30 days"
</Directory>
But it's throwing me a 500 error.
I'm running Apache/PHP through Vista, and mod_expires is enabled in apache.
Here's my full .htaccess file (it works without the <Directory>...</Directory> block)
Code:
# Deny OR Allow Folder Indexes.
# Since we disable access to PHP files you
# can leave this on without worries.
# OR better yet, create a .htaccess file in
# the dir you want to allow browsing and
# set it to +Indexes
Options -Indexes

Options +FollowSymLinks

# Set the default file for indexes
# DirectoryIndex index.php

<IfModule mod_rewrite.c>
    # mod_rewrite rules
    RewriteEngine on

    # The RewriteBase of the system (if you are using this sytem in a sub-folder).
    # RewriteBase /CodeIgniter_1.6.3/

    # This will make the site only accessible without the "www."
    # (which will keep the subdomain-sensitive config file happy)
    # If you want the site to be accessed WITH the "www."
    # comment-out the following two lines.
    # RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
    # RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]

    # If a controler can't be found - then issue a 404 error from PHP
    # Error messages (via the "error" plugin)
    # ErrorDocument 403 /index.php/403/
    # ErrorDocument 404 /index.php/404/
    # ErrorDocument 500 /index.php/500/

    # Deny any people (or bots) from the following sites: (to stop spam comments)
    # RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
    # RewriteCond %{HTTP_REFERER} porn\.com
    # RewriteRule .* - [F]
    # Note: if you are having trouble from a certain URL just
    # add it above to forbide all visitors from that site.

    # You can also uncomment this if you know the IP:
    # Deny from 192.168.1.1

    # If the file is NOT the index.php file
    # RewriteCond %{REQUEST_FILENAME} !index.php
    # Hide all PHP files so none can be accessed by HTTP
    # RewriteRule (.*)\.php(s?|[0-9]*)$ index.php/$1

    # Protect application and system files from being viewed
    RewriteRule ^(application|modules|system) - [F,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !^repos.*
    RewriteCond %{REQUEST_FILENAME} !^user_guide.*
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

<Directory /admin>
    ExpiresActive On
    ExpiresDefault "modification plus 30 days"
</Directory>

# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>
#2

[eluser]Colin Williams[/eluser]
Your .htaccess instructions instruct Apache, not CodeIgniter, which you have handling all requests for '/admin' (and your third and fourth RewriteCond appear superfluous).

What you would need to do is server the expires headers from CI, which you can do with PHP or via CI's Output class (check the docs). Could be done in the constructor of your Admin controller class
#3

[eluser]simshaun[/eluser]
Yes the repos and user_guide cond arent needed.
It's obvious that htaccess doesn't control CI, and I'm not trying to.
What I'm trying to do essentially is enable caching for anything under a certain directory in the URI.
I assumed it's possible, but I guess not.

I already planned on trying to send the cache params through header via a base controller that all the admin controllers extend from. I thought it would have been easier to do it in .htaccess.

Thanks though.
#4

[eluser]Colin Williams[/eluser]
Quote:It’s obvious that htaccess doesn’t control CI, and I’m not trying to ... I thought it would have been easier to do it in .htaccess.
#5

[eluser]simshaun[/eluser]
I'm assuming that is supposed to be a jab at me, as if I'm contradicting myself, but I'm not.
Care to make a reply that actually contains more info than what I've already stated?
#6

[eluser]Colin Williams[/eluser]
I just thought it needed clarification. And I don't know what more to offer. It's clear that your need to serve the expiration headers from CI. The higher up you serve them (controller, base controller, hook, front controller...) the more broad the application will be.
#7

[eluser]simshaun[/eluser]
Thankyou. Yea the idea is to force caching on (all) files located inside admin.
#8

[eluser]yelirekim[/eluser]
My first question is: do you have an actual directory called /admin/, or is Admin one of your controllers? If this is the case then just disregard what I'm saying below and think a little bit about this: you're trying to apply an apache directive to a folder which doesn't exist on the server.

If admin is actually a folder, I don't think you can put a <directory> tag within another <directory> tag, which is essentially what you are doing here.

When you put an .htaccess file inside a folder, apache appends a <directory> tag to your directives for the folder that the .htaccess file is inside. This isn't precisely what happens, but it's accurate for illustrative purposes. Anyways, I think what you should probably do is just create another .htaccess file inside the /admin folder... It should work fine.
#9

[eluser]simshaun[/eluser]
/admin is not an actual directory.

At first I was attempting <Directory> but then found <LocationMatch>, which is how I would achieve this.
The only thing is, both <Directory> and <LocationMatch> are not allowed in .htaccess, so Apache's httpd.conf file needs to modified instead.

So, I use a <LocationMatch> tag and I came up with this: (which I'm pretty sure works):
Code:
<LocationMatch "/admin">
    # 2 weeks
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Cache-Control "max-age=1209600"
    </FilesMatch>
</LocationMatch>
#10

[eluser]yelirekim[/eluser]
I wasn't even aware <LocationMatch> was a valid apache tag, learn something new every day I guess Smile

Glad you got it worked out.




Theme © iAndrew 2016 - Forum software by © MyBB