Re: RewriteCond and negative matching

Dave Wreski <[email protected]> Mon, 14 Apr 2025 12:17:30 -0400
Newsgroups gmane.comp.apache.user
Organization Guardian Digital, Inc.
Message-ID <[email protected]>
>
>>     If the "home page" is an index file in the documentroot, a simple
>>     FallbackResource will suffice, and you don't need to use
>>     mod_rewrite at all.
>
>     I believe that only works for physical files, like
>     /images/myimage.gif, and not for virtual paths set up by Joomla,
>     like /news/myarticle.html, correct?
>
>     I tried just adding it in the VirtualHost, but would like it to
>     work for specific paths, not for any link that doesn't exist on
>     the entire site.
>
>     Thanks,
>     Dave
>
> If you need the complexity of mod_rewrite, I would start by using the 
> rewrite log to troubleshoot.
> This will allow you to see the requested URI path, and rewritten path, 
> if applicable.

Yes, here is that relevant output, although I don't understand how it's 
failing.

RewriteCond: 
input='/news/security-trends/how-ai-is-shaping-the-future-of-linux-administration' 
pattern='^/$' => not-matched
RewriteCond: 
input='/news/security-trends/how-ai-is-shaping-the-future-of-linux-administration' 
pattern='^/(news|features|newsletters)(.*)?$' [NC] => matched
pass through 
/news/security-trends/how-ai-is-shaping-the-future-of-linux-administration
add path info postfix: /var/www/vhost/html/news -> 
/var/www/vhost/html/news/security-trends/how-ai-is-shaping-the-future-of-linux-administration

Here again is the rewrite rule. The pattern matched correctly, so it 
should just result in a 200, but it instead is redirecting. I believe 
the problem is with the first rewriterule, as it is the second one that 
is causing the redirect. It appears that the first rewriterule is not 
enough for it to stop processing rules altogether. Instead, it continues 
on to the second one anyway.

# 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]