Welcome to WebmasterWorld Guest from 18.207.132.114

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Apache 2.4 RewriteRule

     
12:36 pm on May 5, 2015 (gmt 0)

New User

joined:Feb 6, 2015
posts: 7
votes: 0


Hi,

I need to force redirect all pages for websites hosted by my web server to HTTPS except for one site which should force all traffic over HTTP. How to write rewrite rule in Apache for this condition?

For example siteA.test.com should be accessed over HTTP only on port 8080
siteB.test.com should be accessed over HTTPS only on port 8443
siteC.test.com should be accessed over HTTPS only on port 8443

This is what I have tried:-
RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^siteA.*$
RewriteRule (.*) https://%{HTTP_HOST}:8443/$1 [R,L]

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^siteA.*$
RewriteRule (.*) http://%{HTTP_HOST}:8080/$1 [R,L]


The problem is that if I access siteA over https it doesn't redirect. siteB redirects to https from http OK.

Many thanks
2:14 pm on May 5, 2015 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Where do you have the rules defined? Because if it is in a <VirtualHost> container that is listening on a non-SSL port then your rule will never be processed. That was my first thought, anyway.
2:33 pm on May 5, 2015 (gmt 0)

New User

joined:Feb 6, 2015
posts: 7
votes: 0


I have that defined in httpd.conf - not in <VirtualHost> tags. However, I do have several virtualhosts defined .. one for each site if that makes a difference.
4:53 pm on May 5, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15944
votes: 890


Seems as if you ought to set up your conditions as OR, not AND (the default). It's easiest if you can merge it with the domain-name canonicalization rule.

For the sites that should be https:

if the sitename IS NOT in such-and-such exact form
OR
if method is not https (using whichever form is most useful to you, whether it's {HTTPS} or port number or, uh, I forget the third one)
THEN
redirect to https:/ /exact-site-name-here

For the sites that should be http:

if the sitename IS NOT in such-and-such exact form
OR
if method is https
THEN
redirect to http:/ /exact-site-name-here

I do have several virtualhosts defined

Does each virtual host listen on all applicable ports? And, vice versa, does each applicable port have all virtual hosts defined?

Aside from the virtual hosts, is there any difference in mod_rewrite use among the different domain names? I'm thinking particularly of inheritance issues; sometimes you end up having to say the same thing twice just to ensure that everyone hears it.