RewriteCond and negative matching

Dave Wreski <[email protected]> Sun, 13 Apr 2025 23:21:11 -0400
Newsgroups gmane.comp.apache.user
Organization Guardian Digital, Inc.
Message-ID <[email protected]>
Hi,

We're getting a ton of 404s for URLs for paths that don't exist, like 
/apps and other nonsense off the root that never existed that I'd like 
to redirect to the homepage.

Here's what I'm working with so far, but it redirects everything to the 
homepage, including valid articles. For example, here are two URLs that 
should be 200s:
https://linuxsecurity.com/news/security-trends/how-ai-is-shaping-the-future-of-linux-administration
https://linuxsecurity.com/news

# Allow the homepage
RewriteCond %{REQUEST_URI} ^/$ [OR]

# Allow specific directories and everything under them (abbreviated list 
for simplicity here)
RewriteCond %{REQUEST_URI} ^/(news|features|newsletters)(/.*)?$ [NC]

# If allowed, stop rewriting
RewriteRule ^ - [L]

# Otherwise, redirect to homepage
RewriteRule ^ https://linuxsecurity.com [R=301,L]

Everthing else, like https://linuxsecurity.com/apps should be redirected 
to the homepage. I've also tried negative matches, like the following.

RewriteCond %{REQUEST_URI} !^/(news|features|newsletters)(/.*)?$ [NC]
RewriteRule    ^ https://linuxsecurity.com [L,R=301,QSD]

Ideas greatly appreciated.

Thanks,
Dave