homepage Welcome to WebmasterWorld Guest from 54.161.220.160
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
One set of rewrite rules in httpd.conf to apply to all virtual hosts
Apply httpd.conf rewrite rules to all virtual hosts
capitang



 
Msg#: 4620519 posted 8:04 am on Nov 1, 2013 (gmt 0)

I have a server hosting a few sites each having its own container:
<VirtualHost *:80>
ServerName xxx.net
ServerAlias www.xxx.net
DocumentRoot /var/www/xxx
</VirtualHost>

The main site is sitting in:
DocumentRoot "/var/www/html"

I have (bad bot) rewrite rules for the main site in httpd.conf which works well.

However I wish to have those rules apply to all virtual hosts... do I have to duplicate the rules in each container or is there someway I can list them once and have them apply?

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4620519 posted 9:57 am on Nov 1, 2013 (gmt 0)

Do you mean literally rewrite rules, using mod_rewrite? Or are you asking about access control in general? Do you also have site-specific rewrite rules, either in those same directories or further down the line in htaccess? Inheritance for mod_rewrite behaves differently than, well, almost everything else in Apache. So there's potentially more than one question.

capitang



 
Msg#: 4620519 posted 10:14 am on Nov 1, 2013 (gmt 0)

Yes literal rewrite rules in httpd.conf which has all the virtualhost containers listed for each domain.

eg: RewriteCond %{HTTP_USER_AGENT} ^(.*)Yandex.* [NC,OR]

this works well for the main site - what I want to do is have those same rules be applied to each virtual host without having to repeat them for each doamin.

I dont plan using htaccess for each domain - pita to have several htaccess with same rewrites for all the domains.

In other words - have one set of rewrite rules in httpd.conf only and have them apply to all domains - no htaccess rewrites.

capitang



 
Msg#: 4620519 posted 11:09 am on Nov 2, 2013 (gmt 0)

I figured it out after spending hours at apache going over the rewrite docs and then some trial and error with my httpd.conf file... if anyone is interested, shoot a response and I will post... right now my head is spinning from info overload and I am not sure I can explain coherently.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4620519 posted 9:29 pm on Nov 2, 2013 (gmt 0)

Did it have anything to do with inheritance? That would have been my first guess, which is why I asked about mod_rewrite specifically.

RewriteCond %{HTTP_USER_AGENT} ^(.*)Yandex.* [NC,OR]

The leading ^(.*) and trailing .* are both unnecessary. Anything involving a simple UA match can also be done in mod_setenvif. Then you don't have to wrangle with inherited rules.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4620519 posted 11:25 pm on Nov 2, 2013 (gmt 0)

you could insert this into each VirtualHost container:
Include /path/to/httpd-badbot.conf
capitang



 
Msg#: 4620519 posted 1:06 am on Nov 3, 2013 (gmt 0)

The server paths are
/var/www/html (for the default site)

then for each virtual host:
/var/www/html/site1
/var/www/html/site2
/var/www/html/site3

To have all sites use the same rewrite rules, I did it this way:

<Directory "/var/www">
AllowOverride None
#the above will disable the use of .htaccess for all sites which in my case I prefer

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond 1.....
RewriteCond 2.....
RewriteCond 3.....
RewriteCond 4.....
RewriteCond 4.....

RewriteRule ^.* - [F,L]
</IfModule>
</Directory>

Then in each VirtualHost container:
<VirtualHost *:80>
ServerName xxx.net
ServerAlias www.xxx.net
DocumentRoot /var/www/xxx
RewriteEngine on
RewriteOptions inherit
</VirtualHost>

Lucy24 ..
Good catch.
I was originally using {REMOTE_HOST} when I coded ^(.*)Yandex.* but switched it at the last minute to {HTTP_USER_AGENT}.
I caught it after I posted and changed it accordingly.
You raise an interesting point about mod_env and was using it before in the original setup for the default site.
However, I decided to move all rules to mod_rewrite simply because I have some complicated conditions that deal with other issues that are best suited for mod_rewrite... and of course I didnt want to mix modules to do the same thing.

phranque ..
I read about that option but discarded it for the simplicity of having everything in one file.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4620519 posted 1:53 am on Nov 3, 2013 (gmt 0)

RewriteRule ^.*
can be reduced to
RewriteRule .?
No need for an anchor since you're not specifying what content has to come first. Using ? instead of * may let the server out of there faster.

Except, wait, this isn't in a <Directory> section is it? That was the whole point of the question. If so, there's no ? or * about it; all you need is . meaning "If there's a request". (A slightly silly "If", since if there is no request then what's the server even doing here, but you have to say something!)

RewriteOptions inherit

Thought so ;)

Then again, if it's your own server you can move on to 2.4 and replace three-quarters of your RewriteRules with <If> envelopes.

capitang



 
Msg#: 4620519 posted 10:31 am on Nov 6, 2013 (gmt 0)

Thankx for the heads up, anything to minimize resources is worth the effort.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved