Search code examples
regexapachemod-rewrite

Need apache regex config for rewrite rule


Here is a scenario of what I am trying to do.

Folder A has folders B and C. Folder B has D, E and index.html. Folder C has index.html

I declared folder B as my DocumentRoot. Declared an Alias for Folder C to point it to /A/C. I want apache to serve index.html in B for any requests coming in to B (/B/D or /B/E or /B/** should return /B/index.html)

Here is the sample virtual host config

<VirtualHost *:8080>
...
DocumentRoot "/usr/local/var/www/A/B" 
Alias "/C" "/usr/local/var/www/A/C" 
<Directory "/usr/local/var/www/A"> 
....
</Directory> 

RewriteEngine On 
RewriteCond %{ENV:REDIRECT_STATUS} !200 
RewriteRule ^/B/.+ /B/ [PT] 
....
</VirtualHost>

With this configuration, I am running in to redirect loop.


Solution

  • You may try this rule:

    RewriteEngine On 
    
    RewriteCond %{ENV:REDIRECT_STATUS} !200 
    RewriteRule ^/?B/(?!index\.html$) /B/index.html [L,NC] 
    

    Then:

    1. Restart your Apache
    2. Test in a new browser or completely clear your browser cache