apache 2.2 – .htaccess redirect of a whole subfolder except a couple urls

First off, my website currently has a properly working .htaccess, which contains the following (commented to make it clear):

Options +FollowSymlinks -MultiViews
RewriteEngine On

# Redirect subdomains to the main domain name (but not for dev environment)
RewriteCond %{HTTP_HOST} ^(.+).example.com (nc)
RewriteRule ^(.*)$ http://example.com/$1 (R=301,L)

# Better dictionary URLs
RewriteCond %{REQUEST_URI} !^/index.php$ (NC)
RewriteCond %{REQUEST_URI} !^/admin.php$ (NC)
RewriteRule ^pages/dictionary/(.*)$ pages/dictionary/page.php?page=$1 (NC)

# Better looking URLs (not strictly enforced though)
RewriteRule ^pages/quotes/((0-9)+)$ pages/quotes/list.php?id=$1 (NC)
RewriteRule ^pages/tasks/((0-9)+)$ pages/tasks/task.php?id=$1 (NC)
RewriteRule ^pages/users/((0-9)+)$ pages/users/profile.php?id=$1 (NC)

# Actively get rid of .php extension
RewriteCond %{THE_REQUEST} s/((^.)+).php (NC)
RewriteRule ^ /%1 (NE,L,R)

# Apply .php to files without a extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((^.)+)$ $1.php (NC)

# Custom 404 page
ErrorDocument 404 http://example.com/404

# Don't allow direct access to sqldump
<Files "sqldump_schema.sql">
  Deny from all

Everything in this .htaccess has been working properly for years. I just added the “Better dictionary URLs” block, which does not break any of the other instructions in this .htaccess, but I am struggling to make these new instructions work.

What I am trying to achieve: 

  1. Going to https://example.com/pages/dictionary/anything actually requests /page.php?page=anything

  2. Going to https://example.com/pages/dictionary/admin requests /admin.php

  3. Going to https://example.com/pages/dictionary/index requests /index.php

What actually happens:

  1. CORRECT: Going to https://example.com/pages/dictionary/anything requests /page.php?page=anything

  2. WRONG: Going to https://example.com/pages/dictionary/admin requests /page.php?page=admin

  3. WRONG: Going to https://example.com/pages/dictionary/index requests /page.php?page=index

As a reminder, this is the block that is not behaving as expected:

RewriteCond %{REQUEST_URI} !^/index.php$ (NC)
RewriteCond %{REQUEST_URI} !^/admin.php$ (NC)
RewriteRule ^pages/dictionary/(.*)$ pages/dictionary/page.php?page=$1 (NC)

It would seem that these two RewriteCond are ignored and/or have no effect, as the RewriteRule is always (properly) applied no matter the URI / page name .

I tried the following things, to no avail:

  • Changing it to RewriteCond %{REQUEST_URI} !^/pages/dictionary/index.php$ (NC)

  • Changing it to RewriteCond %{REQUEST_URI} !^/index$ (NC)

  • Changing it to RewriteCond %{REQUEST_URI} !^index

And a few more similar tweakings of the path, but all seemed to get ignored by the RewriteRule.

Sorry for the long message but I would rather clarify everything than give half the information.

Thanks in advance to anyone willing to help.