2

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.

5
  • RewriteRule ^/?test/.+ /test/ [NC,L] should work
    – anubhava
    Commented Dec 12, 2023 at 20:39
  • Thanks for the reply @anubhava. We don't want to use NC as we have other rules that should be processed. When I try the above rule with [PT] pass through flag, it runs in to recursion problem. .+ should check if one or more characters exist, but it somehow runs in to recursion.
    – vijay
    Commented Dec 14, 2023 at 21:09
  • Ok, add RewriteCond %{ENV:REDIRECT_STATUS} !200 condition before RewriteRule
    – anubhava
    Commented Dec 14, 2023 at 21:27
  • Can you add these details in questions by clicking on edit link below your questions
    – anubhava
    Commented Dec 18, 2023 at 2:40
  • Thanks for the reply @anubhava. I updated question. I can attach my sample httpd-vhosts.conf, if it helps.
    – vijay
    Commented Dec 18, 2023 at 3:18

2 Answers 2

0

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
1
  • 1
    In the RewriteRule it must be redirected to /B/index.html instead of /B/ . I thought it would pick index.html by default. That did the trick. Thanks for being responsive and formatting the questions!!
    – vijay
    Commented Dec 18, 2023 at 14:28
0

the original request path,"/test/c/1", will match with "^/test/[abc].+$", matched successfully, so the request path change from "test/c/1" to "/test/". but due to the PT flag, cause the "/test/" routed back as if "/test/" is a newly come in, so "/test/" will match with "^/test/[abc].+$" again, but this time match failed, so you got 404.

change the flag from PT to L, the behavior is while the original request path,"/test/c/1", match with "^/test/[abc].+$" successfully, the redirected path "/path" will be the final path. apache get the resource related "/test/" and response to the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.