1

I have a about 25000 urls from my old site that have abc in the url.

I want to use 3 DBM files in my apache conf.

The goal is if the URL does not contain abc ie. http://example.com/1234.htm then I do not want the abcredirects.DBM file to be look at.

If the URL does contain abc ie. http://example.com/abc1234.htm then I only want the abcredirectsDBM file to be look at.

I am trying to use an if statement in my Apache however anytime I use the <If>block whatever I put inside seems to be ignored.

I have changed the <If "%{REQUEST_URI} =~ m#^abc#"> line many different ways but it does not matter. If i remove the <if> block all redirects works as expected.

Why is my <if> block being ignored? There is no error and according to the logs http://example.com/abc1234.htm(i am assuming) is not evaluating to true so I am not sure what else to check.

[root@mail conf]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Jun 27 2018 13:48:59


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteMap abcredirects "dbm:/etc/httpd/conf/dbm/abcredirects.dbm"
        <If "%{REQUEST_URI} =~ m#^abc#">
        RewriteCond ${abcredirects:$1} !=""
        RewriteRule ^(.*) /${abcredirects:$1} [R=301,L]
        </If>
    RewriteMap shortalias "dbm:/etc/httpd/conf/shortalias.dbm"
    RewriteCond ${shortalias:$1} !=""
    RewriteRule ^(.*) /${shortalias:$1} [R=301,L]
</IfModule>

<VirtualHost *:80>
    DocumentRoot /var/www/sites/me
    ServerName example.com
    DirectoryIndex index.htm
    Options +FollowSymLinks
    RewriteEngine On
    RewriteOptions Inherit
    RewriteMap otherredirects "dbm:/etc/httpd/conf/otherredirects.dbm"
    RewriteCond ${otherredirects:$1} !=""
    RewriteRule ^(.*) /${otherredirects:$1} [R=301,L]
</VirtualHost>

Serverfault questions I have seen and tried:

apache force if url has specific pattern redirect to https

Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask

Donna Delour
  • 414
  • 5
  • 10
  • Have you tried re-ording the `IF`s like this: ` ` – Tim Nov 20 '18 at 23:40
  • @varlogtim That did not work, the RewriteMap is not allowed inside of the IF statement, Thank you for trying – Donna Delour Nov 21 '18 at 14:49
  • Why do you have half your config outside the vHost (in a _server_ context)? (And then having to mess around with mod_rewrite inheritance?) – MrWhite Nov 21 '18 at 18:54
  • @MrWhite I did originally have all the rewrites in the main Vhost, however when trying to isolate and not use dbms when not needed, and googling, and throw pasta at the wall seeing what sticks so of the items have moved around, putting all of the rewrites in the main Vhost still results in the same issue of if I use an `` whatever is inside seems to be ignored. Removing the `` the rewrites work however from the logs I can see that both DBM files are being accessed. – Donna Delour Nov 21 '18 at 20:02

1 Answers1

2

<If "%{REQUEST_URI} =~ m#^abc#">

The REQUEST_URI server variable always starts with a slash (the start of the URL-path), so the regex m#^abc# will never match here. You would need to use the regex m#^/abc# instead. (Assuming that wasn't one of the "different ways" you had already tried?)

I would also avoid splitting your config between the vHost and the server - unless there is a specific requirement to do so? This complicates matters with regards to mod_rewrite as the directives in a virtualhost context will ordinarily override the directives in the server context, so you then need to employ mod_rewrite inheritance (which you are doing) - but this gets unnecessarily messy. (The inherit option inherits the parent directives behind the current context, which may not be what you are expecting.)

The rest of your question seems to cross-over with your more recent question:

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • Thank you, however using the method above does not get the URL's to work properly, once again, whatever is in the `IF` is ignored as if the regex or syntax is equating to false. I am looking around to make sure there is not a specific module that needs to be installed or what I could have missed. `Apache/2.4.6 (CentOS)` – Donna Delour Nov 26 '18 at 14:45