6
votes

I have a specific problem about the redirection of HTTPS to HTTP for several pages.

I have Apache2 in front of Zope.

Here's the configuration of my VirtualHost on port 80 :

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName website.com
    ServerAlias www.website.com

<IfModule mod_rewrite.c>
RewriteEngine On

# www to non www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^/(.*) http://website.com/$1 [R=301,L]

# HTTP TO HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>

</VirtualHost>

After, As Zope listens to SSL 8443 port, I apply the following iptables rule :

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443

So my rewrite rule redirects 80 to 443 and iptables redirects 443 to 8443.

In this context, how could I redirect one page (say "https://website.com/wiki/test_page.html") to "http://website.com/wiki/test_page.html".

I tried to add the following rule :

# if https on then turn off
RewriteCond %{HTTPS} on
RewriteRule ^/wiki/test_page.html http://%{HTTP_HOST}%{REQUEST_URI}

but it doesn't work.

Instead of doing another rewrite rule, I think that it should be easier to prevent, for the page "/wiki/test_page.html", the HTTPS redirection, but I don't know how to achieve this.

Any advice is welcome,

Thanks

UPDATE 1 :

I tried :

# HTTP TO HTTPS except page "/wiki/test_page.html"
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}

without success

UPDATE 2 :

I have made progress in my issue. My temporary solution is to apply :

RewriteEngine On

# www to non www and HTTP to HTTPS redirection for all website
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page\.html [NC]
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]

# rewrite HTTPS to HTTP for test_page.html
#RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

With these rules, I can browse all my website with HTTPS links except for /wiki/test_page.html page. For this, I put, into the page containing the link "/wiki/test_page.html", an explicit HTTP href link :

<a class="bottom_link" href="http://website.com/wiki/test_page.html">Test page</a>

in this way, the last rewrite rule is applied ( RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L] ) and the request on 80 port is forwarded to 9674 HTTP port of Zope3.

In the following RewriteCond, I made you notice that RewriteCond %{HTTPS} on is not matched when I browse all my HTTPS website.

# rewrite HTTPS to HTTP for test_page.html
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
    RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

Is it because Apache2 can't detect the HTTPS port of Zope3 (8443 or 443) ?

Now, I would like to directly forward HTTPS request on /wiki/test_page.html to HTTP request on Zope3 server (which is on 9674 port). I think the solution could come from the modification of this rewrite rule :

RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

I tried for example to modify it as follow :

RewriteRule ^/(.*)  http://localhost:9674/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]`

or

RewriteRule ^/(.*)  http://localhost:9674/++vh++https:%{SERVER_NAME}:8443/++/$1 [P,L]

unfortunately, this doesn't work : for example, if I click on the link "https://website.com/wiki/test_page.html" from the page containing this link, the URL is not rewritten for both above RewriteRule.

Any help is welcome

1
what happens when you use just http on that single page?Panama Jack
with the above rewrite rules (on UPDATE), website.com/wiki/test_page.html is redirected to website.com/wiki/test_page.html and I don't understand why ?youpilat13

1 Answers

3
votes

Well the rules look fine to me inside your vhost file. Make sure you have restarted apache after each change and also clear your cache because 301 redirects get cached by the browser. Also you can combine your rules into 1 like below. That should work.

# www to non www and http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC, OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html [NC]
RewriteRule ^/?(.*) https://website.com/$1 [R=301,L]