Issue domain.map file being ignored in Apache & nginx Settings

rgfincher

Basic Pleskian
Server operating system version
Ubuntu 22.04
Plesk version and microupdate number
18.0.76 Update #6
I am attempting to use a domain.map file full of redirects.
The location of the file is : /var/www/vhosts/system/MyDomain/conf/domain.map
I am putting the following into the Apache & Nginx settings :

RewriteEngine On
RewriteMap redirects txt:/var/www/vhosts/system/$MyDomain/conf/domain.map
RewriteCond ${redirects:/$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ https://www.MyDomain${redirects:/$1} [R=301,L,NE]

The file is being read, because I can see it in lsof output
The file ownership and permissions are :
-rw-rw-r-- 1 www-data www-data 1379486 Jun 17 21:02 domain.map

No errors appear to be generated in any log files I can find, including the ones in /var/log/apache2/error.log and /var/log/plesk-php??-fpm/error.log

I have tried temporarily disabling the Web Application Firewall

Grateful for any comments.
 
A few things jump out immediately.


1. RewriteMap cannot normally be used in per-vhost or .htaccess context​


RewriteMap is only valid in the Apache server config context (httpd.conf, included server config files, etc.), not inside a <VirtualHost>, .htaccess, or many hosting-panel custom rewrite sections.

If you're putting this into Plesk → Apache & Nginx Settings → Additional Apache directives, Apache may silently ignore it or fail to load it depending on where Plesk injects the directive.

Check the generated vhost config:

Code:
apachectl -S
grep -R "RewriteMap redirects" /var/www/vhosts/system/MyDomain/conf/

Also run:

Code:
apachectl -t
apachectl -t -D DUMP_RUN_CFG

If RewriteMap is in an invalid context, Apache usually reports something like:
RewriteMap not allowed here
during a config test.

2. $MyDomain is probably wrong​


You have:

Code:
RewriteMap redirects txt:/var/www/vhosts/system/$MyDomain/conf/domain.map

Use the actual path:

Code:
RewriteMap redirects txt:/var/www/vhosts/system/example.com/conf/domain.map

or whatever the real domain is.

The fact that you see the file open in lsof suggests Apache may be reading some map file, but I'd still verify the exact path in the generated configuration.

3. Your rule syntax may not be matching what is in the map​

You are looking up:

Code:
${redirects:/$1}
Notice the leading slash.

So if the request is:
Code:
/old-page
the lookup key becomes:
Code:
/old-page
Your map file must contain entries like:

Code:
/old-page /new-page
/about-us /company/about

If your map contains:

Code:
old-page /new-page

(without the leading slash), every lookup will fail.

4. Turn on rewrite logging​


For Apache 2.4:
Code:
LogLevel warn rewrite:trace8
Then inspect the vhost error log.

You'll see lines such as:

Code:
map lookup OK
map lookup FAILED
applying pattern

which immediately reveal whether the map is being consulted and what key Apache is searching for.

5. Verify mod_rewrite is actually loaded​

Check:

Code:
apachectl -M | grep rewrite
You should see:
Code:
rewrite_module

6. The condition is unusual​

You have:

Code:
RewriteCond ${redirects:/$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ https://www.MyDomain${redirects:/$1} [R=301,L,NE]

In Apache, conditions are evaluated before the rule pattern is matched, so $1 can be problematic here because the capture comes from the RewriteRule.

A more reliable approach is:

Code:
RewriteCond ${redirects:%{REQUEST_URI}|NOT_FOUND} !NOT_FOUND
RewriteRule ^ https://www.example.com${redirects:%{REQUEST_URI}} [R=301,L]

This avoids dependence on rule backreferences entirely.

7. Confirm the generated Plesk configuration​


Plesk often regenerates Apache configs. After applying changes, inspect:
Code:
cat /var/www/vhosts/system/MyDomain/conf/httpd.conf
or

Code:
grep -n RewriteMap /var/www/vhosts/system/MyDomain/conf/httpd.conf
to ensure the directive actually ended up where you think it did.

My strongest suspicion is either:
  1. RewriteMap is being placed in a context where Apache does not allow it, or
  2. $1 is empty in the RewriteCond, causing every lookup to return NOT_FOUND.

The first thing I'd test is replacing your condition with:

Code:
RewriteCond ${redirects:%{REQUEST_URI}|NOT_FOUND} !NOT_FOUND
and enabling:

Code:
LogLevel rewrite:trace8

because the rewrite trace will immediately show what key is being looked up in domain.map.
 
Thanks - lots of good stuff there, I will work through it.
A few brief notes - where I've put $MyDomain, that is just me, anonymising the contents of the configuration to remove my customer's domain, equivalent to your example.com

The result of
# apachectl configtest
Syntax OK

apachectl -M | grep rewrite
rewrite_module (shared)

Re the contents of the domain.map file,
I confirm that both column1 and column2 in the file begin with a leading /
 
Back
Top