Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I do this?

I found this but my question is the same: how can I do this without resorting to .htaccess files and performing some other action for each virtual host?

OMGTIA!

like image 871
hourback Avatar asked Dec 05 '08 17:12

hourback


People also ask

What is RewriteCond in Apache?

RewriteCond is an apache mod-rewite directive which is used for conditional URL rewriting on Apache server. We use RewriteCond along with RewriteRule%{HTTP_HOST} , %{HTTPS} etc.

What is the command to enable mod rewrite in Apache?

In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.

How do you write rewrite rules in httpd conf?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

How do I enable mod rewrite in Webmin?

One of the easiest would be to go into Webmin -> Servers -> Apache -> Global configuration -> Edit Config Files, and to perform the edits from there.


3 Answers

Specify RewriteOptions InheritDown in the parent scope (such as httpd.conf) to get your rules applied in child Virtual Hosts without modifing them.

This will only work on Virtual Hosts where the RewriteEngine directive is set to on:

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.

(source)

Apache supports this since 2.4.8 (not available at the time of the original question).

From documentation for RewriteOptions:

InheritDown

If this option is enabled, all child configurations will inherit the configuration of the current configuration. It is equivalent to specifying RewriteOptions Inherit in all child configurations. See the Inherit option for more details on how the parent-child relationships are handled. Available in Apache HTTP Server 2.4.8 and later.

InheritDownBefore

Like InheritDown above, but the rules from the current scope are applied before rules specified in any child's scope. Available in Apache HTTP Server 2.4.8 and later.

IgnoreInherit

This option forces the current and child configurations to ignore all rules that would be inherited from a parent specifying InheritDown or InheritDownBefore. Available in Apache HTTP Server 2.4.8 and later.

(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions)

like image 109
Matej Snoha Avatar answered Oct 05 '22 23:10

Matej Snoha


By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <VirtualHost> section:

RewriteEngine On
RewriteOptions Inherit 

click http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html to find more information

like image 26
Zava Avatar answered Oct 06 '22 00:10

Zava


Looks like the simplest possible solution is to add

RewriteOptions inherit

to each VirtualHost directive. This is at least a lot simpler than messing with .htaccess files. Apache is pretty clear on the fact that

by default, rewrite configurations are not inherited. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use it. (http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)

and apparently the way to change the default is via RewriteOptions in the child (vhost or director), so you have to do something in each child.

like image 44
Travis Wilson Avatar answered Oct 05 '22 22:10

Travis Wilson