Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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/
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
Thank you! I posted a second attempt above using a directory context. For some reason, Apache now seeks files in /var/www. –  David Faux May 31 '12 at 19:24
    
@DavidFaux It's using the default site; you probably want to modify the default site file instead of creating a new one. –  Shane Madden May 31 '12 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.