Move RewriteBase from htaccess to site conf doesn't work

Viewed 394

This is my original .htaccess file. The rewrite rule works fine:

RewriteEngine On
RewriteBase /backenddev/
RewriteRule ^share/([ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789]{8})$ share.php?token=$1

But after moving the RewriteBase part into Apache's site conf file and restart Apache, it doesn't work anymore.

The new .htaccess file with the rewrite rule only:

RewriteRule ^share/([ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789]{8})$ share.php?token=$1

And the Apache site conf file:

  Alias /backenddev "/var/www/backend_dev/"
  <Directory /var/www/backend_dev/>
    Options FollowSymLinks
    RewriteEngine On
    RewriteBase "/backenddev/"
    AllowOverride All
    
    ...
  </Directory>

When I open the URL https://<mysite>/backenddev/share/XV6TqNpE I get an 404 and the site content is:

The requested URL /var/www/<mysite>/backend_dev/share.php was not found on this server.

A redirection to https://<mysite>/backenddev/share.php?token=XV6TqNpE is expected.

Any ideas why it doesn't work?

1 Answers

The value of the RewriteBase directive defined in parent configs is not inherited by child configs by default*1. You need to explicitly enable this with the MergeBase option in the parent config.

For example:

<Directory /var/www/backend_dev/>
  Options FollowSymLinks
  RewriteEngine On
  RewriteBase /backenddev/
  RewriteOptions MergeBase
  AllowOverride All  
  ...
</Directory>

Note that child configs can still override this by setting there own RewriteBase.

*1 This default behaviour actually changed in Apache 2.4.4. Between 2.4.0 and 2.4.3 the value of the RewriteBase was apparently merged into child configs by default.

Reference:

MergeBase
With this option, the value of RewriteBase is copied from where it's explicitly defined into any sub-directory or sub-location that doesn't define its own RewriteBase. This was the default behavior in 2.4.0 through 2.4.3, and the flag to restore it is available Apache HTTP Server 2.4.4 and later.

Note, however, that this option alone does not enable mod_rewrite inheritance. Directives in the child (.htaccess) configs will still completely override directives in the parent <Directory> container. The MergeBase option refers only to the value of RewriteBase.


Aside:

RewriteRule ^share/([ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789]{8})$ share.php?token=$1

This could be "simplified" to:

RewriteRule ^share/([a-np-z1-9]{8})$ share.php?token=$1 [NC]

Since only the o, O and 0 are omitted from the character ranges.

The NC flag would then be required to match the corresponding uppercase letters.

Also, consider adding the L flag since you have "other rules" - and these will likely be unnecessarily processed (assuming there is no conflict already).

Related