I've taken the Apache Server Configs global .htaccess file and placed it on my web server, as /etc/apache2/apache-server-configs.conf. Then in my /etc/apache2/apache2.conf file I've got this towards the end of my file:

# Include of directories ignores editors' and dpkg's backup files,
# see the comments above for details.

# Apache Server Configs
Include apache-server-configs.conf

# Include generic snippets of statements
Include conf.d/

# Include the virtual host configurations:
Include sites-enabled/

The file apache-server-configs.conf is being included properly, yet this block of code fails to work:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

(there is a RewriteEngine on statement above this block, several lines up) If I put that within a single domain's .htaccess file, it works as expected. But on a global level, it does nothing. I attempted to debug using RewriteLog and the log file was empty when I visited my website.

What am I missing?

Debian 7.5, apache 2.2.22

EDIT 8/2/2014: Adding more info

It's also worth noting that if I include a conf file in /etc/apache2/apache2.conf and I put any RewriteRule calls in the included file, none of them work, period. Other things within the conf file work just fine, but rewrites do not.

test.com virtual host file:

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@test.com
  ServerName  test.com
  ServerAlias www.test.com
  Options -Indexes

  <IfModule mpm_itk_module>
    AssignUserID bob bob

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /home/bob/www/test.com

  # Log file locations
  LogLevel warn
  ErrorLog  /home/bob/logs/test.com/error.log
  CustomLog /home/bob/logs/test.com/access.log combined

<VirtualHost *:443>
     SSLEngine On
     SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
     SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
     #SSLCertificateChainFile /etc/ssl/test.com/ca-bundle

     ServerAdmin webmaster@test.com
     ServerName test.com
     ServerAlias www.test.com
     Options -Indexes

     <IfModule mpm_itk_module>
       AssignUserID bob bob

     DocumentRoot /home/bob/www/test.com
     ErrorLog /home/bob/logs/test.com/error.log
     CustomLog /home/bob/logs/test.com/access.log combined
  • Have you tried using RewriteEnging on before those settings? Jul 28 '14 at 5:48
  • @TeroKilkanen Yes, there is a RewriteEngine on statement above that block. Jul 28 '14 at 5:50

According to the Apache docs, rewrite conditions are not by default inherited by child Virtualhost blocks. Taken from the docs, it is suggested that they can be set to inherit like so:

RewriteEngine On
RewriteOptions Inherit

You would have to set this on each Virtualhost that needs to inherit the Rewrite rules you've set in the main configuration file.

  • I'm afraid that didn't work. Jul 28 '14 at 7:37
  • It should. Could you post an excerpt of the relevant parts of your Virtualhost block? [cough]Anddidyourestart?[/cough] Jul 28 '14 at 8:18
  • Yes, definitely reloaded/restarted. I've posted the virtual host conf file above. Aug 2 '14 at 7:19
  • Oops, I did it wrong! Now it works :) Aug 2 '14 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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