Home Stop virtualhost rewrite rules executing if a rule was matched in the global config
Reply: 1

Stop virtualhost rewrite rules executing if a rule was matched in the global config

Mike Pengelly
Mike Pengelly Published in 2017-11-13 13:25:51Z

I have the following setup. Apache running with a separate VirtualHost file for each site. Each of these has their own set of rewrite rules, for http to https for example which is all running fine.

What we would like to happen is this, from the global config we need to be able to check if a request is for a particular subdirectory. If it is then we should allow this request to process as it should but at that point we do not want the individual virtual host file rewrite rules to kick in. Therefore allowing this directory to be served on non https connections and not be redirected to https.

I have set up the rewrite rules and can match on the directories and redirect to an external url if it matches from the global which shows its inheriting but if I try to just allow it through the virtual hosts rewrites kick in and it redirects.

I have tried using L and END but this did not work either. Is there any way of achieving this without editing the virtual host files that are already configured?

Main httpd config entry

<Directory "/www">
Options Indexes Includes FollowSymLinks MultiViews
        AllowOverride All
        allow from all
        Order allow,deny
        Require all granted

RewriteEngine On
RewriteOptions InheritDownBefore

RewriteCond %{REQUEST_URI} ^/sub_directory/$ [NC]
RewriteRule ^(.*) $1  [L,END]
#RewriteRule ^(.*) -  [L,END]
#RewriteRule ^(.*) http://www.google.com  [L,END]  # This does get triggered


sample virtual host file.

<VirtualHost *:80>
ServerName urlone.com
ServerAlias urltwo.com

DocumentRoot /www/

RewriteEngine On

# redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://urlone.com$1 [R=301,L]


so if I visit urlone.com it should redirect to https://urlone.com but if I visit urlone.com/sub_directory it needs to not allow the redirect to https.

I hope this makes sense to someone and thanks in advance for any help.

Dusan Bajic
Dusan Bajic Reply to 2017-11-13 15:38:46Z

In global httpd.conf:

RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/sub_directory$ [NC]
RewriteRule ^ - [E=PATH_MATCHED:true]

(if needed, you can add additional rules, or additional flags to the above rule)

In virtual_host.conf

RewriteCond %{ENV:PATH_MATCHED} !=true
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://urlone.com%{REQUEST_URI} [R=301,L]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312784 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO