Icebergs
Stock Image

Excluding a Subfolder In .htaccess

I'm fundamentally lazy when it comes to running infrastructure management tools. I don't like running a ton of virtual machines that I have to keep updated and backed up.  I'd much rather run a collection of web apps on a single server. Since the user count is pretty small I can get away with a fairly low powered machine to handle most of my tools.  Right now, I have a small Ubuntu server running on Hyper-V and it's running the following:

They're all humming away like a song.  That wasn't always the case.  I just added Snipe-IT to the environment at the root of the web server. (Not so) Coincidently, OCS Inventory stopped collecting data from agents the exact same day I installed Snipe-IT. *sigh.  As it turns out, virtual directories aren't treated the same way as real folders. So the /ocsreports page continued to work but /ocsinventory (the virtual directory for processing inventory agent communication) was caught in the Snipe IT rewrite rules defined in the .htaccess file and started throwing a 404 message.

What a pain.  The good news is that it pretty easy to fix this.  All you have to do is add the following rewrite condition ahead of each block of rewrites that are in the .htaccess and you're good to go.

RewriteCond %{REQUEST_URI} !^/your-directory.*$

This line says, "If the URL doesn't contain '/your-directory' you may continue with the rest of the rewrite conditions and rules.  If it does, stop. Essentially, it prevents the root web app from applying any rewrite conditions to /your-directory which will fix most of the issues you'll run into when you're running multiple web apps on a single server and using directories to seperate them. I seem to recall WordPress could cause this issue as well.

So here's what the redirect rules for Snipe IT look like in order to allow OCS Inventory work:

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_URI} !^/ocsinventory.*$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_URI} !^/ocsinventory.*$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

I sure hope this'll save you some time.

This article was updated on May 4, 2021

Rob Tacey

Rob is the IT Systems Manager for a manufacturing automation company in Southwestern Ontario. It's great. He's a technologist focusing on information technology, IT security, and customer satisfaction. With over 20 years of experience in various IT roles, it might actually be worth reading some of his stuff.

Comments