Score:1

Apache rewrite rule for a folder to a different domain

mf flag

I need to redirect all requests concerning a folder to another (old) web server called www2 keeping the entire original path, for example

https://www.example.org/thisfolder/maps/map.jsp?id=2345 must be redirected to https://www2.example.org/thisfolder/maps/map.jsp?id=2345

I think the solution is to use the mod_rewrite module with RewriteRule directive and should be quite simple, but I cannot find a working example. I tried with

<IfModule !mod_rewrite.c>
    LoadModule rewrite_module  modules/mod_rewrite.so
</IfModule>
<Directory "/thisfolder">
    RewriteEngine On
    RewriteBase "/thisfolder"
    RewriteRule "(.*)" "https://www2.example.org/$1"
</Directory>

This is an Apache2.4 installation running on Linux CentOS 7.6 (Red Hat Enterprise). Thank you so much for your help!

Score:0
by flag

The <Directory> directive works for filesystem paths, not URL paths, so we need to change it to <Location> and also we have to capture properly the the request after "thisfolder/".

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule "^/thisfolder/(.*)" "https://www2.example.org/thisfolder/$1" [R=301,L]
</IfModule>
Score:0
in flag

Note that such a simple redirect requirement is a text book case where you don't need mod_rewrite.

mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain.

A simple Redirect directive will already be sufficient in your case:

 Redirect "/thisfolder" "https://www2.example.org/thisfolder"
Steph avatar
mf flag
Thank you HBruijn. So easy :)
HBruijn avatar
in flag
Indeed, not complicated at all. - Once you know where in the extensive documentation for Apache httpd you need to look though ;)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.