Using rewrite rules across multiple virtual hosts

Using a single web hosting account to host multiple sites

Using rewrite rules across multiple virtual hosts

Postby AbuAaminah » Thu Dec 18, 2008 7:58 am

We have about 15 lines of rewrite rules which we want to apply to all subdirectories.

The only problem is that, from the Apache manual,

"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."

So how do we use those rewrite rules on all the site's subdirectories without copy/pasting them into htaccess files in every subdirectory?

Thanks for your great help.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Thu Dec 18, 2008 8:27 am

It means they are not inherited to other <VirtualHost>s. They are inherited within sub directories of one <VirtualHost>.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Thu Dec 18, 2008 9:49 am

I meant to say "subdomains". I'd like to the same rewrites to apply to all the subdomains.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Thu Dec 18, 2008 12:42 pm

You can inherit mod_rewrite from the main server configuration (not other <VirtualHost>s) using
Code: Select all
RewriteOptions Inherit
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Thu Dec 18, 2008 7:20 pm

Where would that go?
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Fri Dec 19, 2008 6:02 am

In the <VirtualHost>(s) you want to inherited the mod_rewrite. Ie. in the sub domain <VirtualHost>s. The mod_rewrite has to be in the main server configuration.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Fri Dec 19, 2008 8:30 pm

Thanks Richard.

When I put the mod_rewrite in <Directory "etc/etc/etc">, it doesn't work at all. Am I putting it in the wrong place?

Thanks
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Sat Dec 20, 2008 10:56 am

Not in a <Directory>. The mod_rewrite goes in your main server configuration (ie. straight in the httpd.conf file not in any <tag>s). The Inherit line goes in the <VirtualHost>.

For example
Code: Select all
RewriteEngine On

# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+\..+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]

<VirtualHost *:80>
  ServerName exampleA.com
  ServerAlias *.exampleA.com

  # Inherit the "Remove www" mod_rewrite.
  RewriteOptions Inherit
</VirtualHost>

<VirtualHost *:80>
  ServerName exampleB.com
  ServerAlias *.exampleB.com

  # Does NOT inherit the "Remove www" mod_rewrite.
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Sat Dec 20, 2008 7:04 pm

Excellent. Thanks.

Does the inherit part need to go in the root directory's tag or is it automatically inherited?
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby AbuAaminah » Sun Dec 21, 2008 5:43 am

Unfortunately, that doesn't work.

The following rewrites are supposed to match extensionless files that are not directory names to their php equivalents:

RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule !(\.)(.*)$ %1.php [L]

But nothing happens.

Your help is very, very much appreciated.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 11 guests

cron