RewriteBase Rule – Works in .htaccess but Not in httpd.conf

.htaccessapache-2.2httpd.confmod-rewriterewrite

Let me explain by example…

File: /var/www/example.com/public/wp-content/cache/minify/.htaccess

<IfModule mod_rewrite.c>

    # THIS WORKS...
    RewriteBase /wp-content/cache/minify/
    RewriteRule /w3tc_rewrite_test$ ../../plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 [L]
    RewriteCond %{REQUEST_FILENAME}%{ENV:APPEND_EXT} -f
    RewriteRule (.*) $1%{ENV:APPEND_EXT} [L]
    RewriteRule ^(.+/[X]+\.css)$ ../../plugins/w3-total-cache/pub/minify.php?test_file=$1 [L]
    RewriteRule ^(.+\.(css|js))$ ../../plugins/w3-total-cache/pub/minify.php?file=$1 [L]

</IfModule>

File: /etc/apache2/httpd.conf

<Directory /var/www/example.com/public>

    AllowOverride None

    Options -MultiViews

    [...]

    <IfModule mod_rewrite.c>
        Options +FollowSymlinks
      # Options +SymLinksIfOwnerMatch
        RewriteEngine On
      # RewriteBase /
    </IfModule>

    [...]

    <IfModule mod_rewrite.c>

        # BUT THIS ISN'T WORKING!!!
        RewriteBase /wp-content/cache/minify/
        RewriteRule /w3tc_rewrite_test$ ../../plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 [L]
        RewriteCond %{REQUEST_FILENAME}%{ENV:APPEND_EXT} -f
        RewriteRule (.*) $1%{ENV:APPEND_EXT} [L]
        RewriteRule ^(.+/[X]+\.css)$ ../../plugins/w3-total-cache/pub/minify.php?test_file=$1 [L]
        RewriteRule ^(.+\.(css|js))$ ../../plugins/w3-total-cache/pub/minify.php?file=$1 [L]

    </IfModule>

    [...]

</Directory>

For performance, I want to disable use of .htaccess on my server, and use the httpd.conf configuration file instead, which is what you can see above.

The thing is, the .htaccess rule which is placed in the specific directory (/var/www/example.com/public/wp-content/cache/minify/) works, but the same rule in my httpd.conf file doesn't. I am not sure why. What could I be doing wrong here?

Best Answer

Not sure if this is the best way to do it, but it works. Essentially, it needs the RewriteEngine On rule in the second <Directory> section as well.

File: /etc/apache2/httpd.conf

<Directory /var/www/example.com/public>
    AllowOverride None
    Options -MultiViews

    [...]

    <IfModule mod_rewrite.c>
        Options +FollowSymlinks
      # Options +SymLinksIfOwnerMatch
        RewriteEngine On
      # RewriteBase /
    </IfModule>

    [...]
</Directory>

<Directory /var/www/example.com/public/wp-content/cache/minify>
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /wp-content/cache/minify/
        RewriteRule /w3tc_rewrite_test$ ../../plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 [L]
        RewriteCond %{REQUEST_FILENAME}%{ENV:APPEND_EXT} -f
        RewriteRule (.*) $1%{ENV:APPEND_EXT} [L]
        RewriteRule ^(.+/[X]+\.css)$ ../../plugins/w3-total-cache/pub/minify.php?test_file=$1 [L]
        RewriteRule ^(.+\.(css|js))$ ../../plugins/w3-total-cache/pub/minify.php?file=$1 [L]
    </IfModule>
</Directory>
Related Topic