[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1172
  • Last Modified:

Apache 2.2 ignoring rewrite rules within .htaccess files unless it is located within the default website directory


We are experiencing a strange problem on our development Apache 2.2 web server.  The default htdocs directory is /www/htdocs, and rewrite rules are processed fine if they reside within a .htaccess file inside that directory, however, when that file is moved to a virtual host, the rewrite rules are not processed, but the .htaccess file is still read as I can cause a 500 error if I put jargon into it and the errordocs are honoured where they have been set.

From this, I'm pretty sure that it is not a fault in the rewrite rules themselves, so it must lie within the httpd.conf file.  I have attached the config files to this post, though identifying domains and email addresses have been replaced with valid placeholders.

If anyone has any idea what could be causing this issue, I would be very grateful.

Thanks in advance,


Edited to add: I'm not a beginner, but there doesn't seem to be any way to change that field after this has been submitted, so more fool me for not filling it out properly in the first place. ;)
  • 4
  • 4
1 Solution
Add the 'RewriteEngine On' in each virtual host section, then restart apache.


"Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules."

IllaraneAuthor Commented:

I tried that and it still won't honour the RewriteRule directives.  The top of the .htaccess file looks like this:

RewriteEngine On
RewriteBase /

Additionally, I get the following appearing in the RewriteLog when I browse to http://www.mydomain.com/login/, which is rewritten to /index.php?page=login: - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9cf83b8/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/login - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d54eb8/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9cfc3c8/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/themes/default/css/theme.css - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9cf83b8/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/include - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d56d10/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9cf83b8/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/favicon.ico - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d51348/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d033e0/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/mysite - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d5c680/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d073f0/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/mysite - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d03920/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d0b400/initial] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/mysite - - [08/Feb/2008:16:10:44 +0000] [dev.mydomain.com/sid#9b24868][rid#9d7aef0/initial/redir#1] (1) [per-dir /www/vhosts/dev.mydomain.com/httpdocs/] pass through /www/vhosts/dev.mydomain.com/httpdocs/index.php

Also, adding RewriteEngine On to the VirtualHost section seems to stop my RewriteLog from working... :/

Now, from your logs and your config I see that your .htaccess rewrite rule work.
It's taken from /www/vhosts/dev.sagazone.co.uk/httpdocs/.htaccess
and _not_ from /www/htdocs/.htaccess

dev.mydomain.com is your default virtual host, because it is your first virtualhost section, it serves all names that don't match ServerName in any other virtualhost.
Directory /www/htdocs is never used for 'document root' because you have enabled NameVirtualHost and defined at least one VirtualHost entry. So www.mydomain.com is also served by dev.mydomain.com virtual domain section.

Read more here:

So your .htaccess is from /www/vhosts/dev.sagazone.co.uk/httpdocs/,
thats the point.

Next, why do I say the rewrite engine is working? Because we see all these entries in your rewrite log. I see lots of 'pass through', that means that nothing has been rewritten.

If you think that some rules doesn't work, you should show all that rules, not only the top of the .htaccess.

Once you have defined 'RewriteEngine On' in a VirtualHost section you should place RewriteLog there as well. The same is for RewriteLogLevel.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

IllaraneAuthor Commented:

The .htaccess file is exactly the same.  If I remove the virtual hosts include from http.conf and put everything in /www/htdocs, it all works fine, but if I enable the virtual hosts and move everything into one of the virtual hosts folders, nothing gets rewritten at all.  The URLs haven't changed, I'm not moving it to something like http://dev.mydomain.com/folder/; it just stops working, so I'm pretty certain that it's not an issue with the .htaccess file.
IllaraneAuthor Commented:
Turns out it was the / on the front of the rewrite checks.  Took this out, and it works fine everywhere now.
Hi, Illaran.

Let me disagree with your last comment.

You were asked to provide full .htaccess config with rewrite rules:
>> If you think that some rules doesn't work, you should show all that rules, not only the top of the .htaccess.

Then you say that it's ok and didn't post .htaccess:
> The .htaccess file is exactly the same.

Now you say that your rewrite checks contain leading '/'
> Turns out it was the / on the front of the rewrite checks.  Took this out, and it works fine everywhere now.

So your last solution about PAQ-ing the question is not much useful for others, because they can't see the reason of why it was not working as intended (because they don't see your .htaccess file here and can't guess what was wrong with it).

For PAQing, I guess that's enough to post your .htaccess (or even one line from it) before and after the change.
IllaraneAuthor Commented:
The rewrite rules looked something like this before:

RewriteRule ^/rewrite/this/to/? /index.php?page=rewrite&mode=this&id=to [QSA,L]

and this after:

RewriteRule ^rewrite/this/to/? /index.php?page=rewrite&mode=this&id=to [QSA,L]

For some reason, Apache interprets the / on the front of the rewrite rule to mean the default htdocs directory as defined within the configuration, not the root of the current virtual host, hence the confusion as to why it worked before from within that directory but not anywhere else.

Removing the / made it all work.

I'd attach the .htdocs files to this comment, but I don't have them to hand.
OK, now I agree that you provided all info to mark this question as PAQ.
Closed, 250 points refunded.
EE Moderator

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now