How can I avoid repeating DocumentRoot in this Apache virtual host?

Posted on

How can I avoid repeating DocumentRoot in this Apache virtual host? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, , , , .

I have an Apache virtual host configured for a website powered by WordPress.

<VirtualHost *:80>
ServerName 67.178.132.253
DocumentRoot /home/david/wordpressWebsite

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index.php$ - [L]
RewriteCond /home/david/wordpressWebsite%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

</VirtualHost>

How can I avoid hard-coding /home/david/wordpressWebsite twice? I don’t want to use REQUEST_URI since that involves an extra request.

Here’s my attempt at using a directory context. I made a file in sites-available with these contents.

DocumentRoot /home/david/wordpressWebsite

<Directory /home/david/wordpressWebsite>
    RewriteEngine On
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    # You need these somewhere, anyway; better to not put them on the root.
    Order allow,deny
    Allow from all
</Directory>

For some reason, Apache is seeking files in /var/www as evinced in my error logs:

[error] [client 69.175.67.64] File does not exist: /var/www/about, 
referer: http://67.178.132.253/

Solution :

For future people who stumble upon this and want an answer to the original question:
How can I avoid repeating DocumentRoot in this Apache virtual host?

The value of the DocumentRoot directive can be accessed by using the server variable
%{DOCUMENT_ROOT}. This means that in the OPs example, instead of writing:

RewriteCond /home/david/wordpressWebsite%{REQUEST_FILENAME} !-f

We can write

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

Therefore, the only place you should hard code your root file path is in the DocumentRoot directive.

Those WordPress rules were built to be inside a <Directory> context, as evidenced by the fact that they’re looking for ^index.php$ without a leading slash; that rule won’t ever match in a <VirtualHost> context.

Once inside a <Directory> context, the REQUEST_FILENAME variable contains the physical file location, as explain in the documentation:

The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

So, try something like this:

<Directory /home/david/wordpressWebsite>
    RewriteEngine On
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    # You need these somewhere, anyway; better to not put them on the root.
    Order allow,deny
    Allow from all
</Directory>

As far as hard-coding it twice (in both the DocumentRoot and <Directory> settings): if you really need to avoid that for some reason, then look into the “config file variables” feature of Apache 2.4.

Leave a Reply

Your email address will not be published. Required fields are marked *