Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite does not work as expected

Redirecting http requests to https

         

dzi57

1:25 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



Hi,

I have some problems with mod_rewrite.
First, what I want to do:
My client have to connect with the URL: [mydomain:7777...]
If by mistake, they enter http rather than https, I wand to make a redirection to https.IE [mydomain:7777...] => [mydomain:7777...]
I add the three Rewrite line in ssl.conf and the redirection doesn't work.

Any help welcome

dzi57

httpd.conf file:
LoadModule rewrite_module libexec/mod_rewrite.so

ssl.conf:
Listen 7777

<VirtualHost _default_:7777>
# General setup for the virtual host
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) [mydomain:7777...] [R=301,L]
DocumentRoot "/http/9.0.4/Apache/Apache/htdocs"
ServerName myserver
ServerAdmin you@your.address
ErrorLog "¦/http/9.0.4/Apache/Apache/bin/rotatelogs /http/9.0.4/Apache/Apache/logs/error_log 43200"
TransferLog "¦/http/9.0.4/Apache/Apache/bin/rotatelogs /http/9.0.4/Apache/Apache/logs/access_log 43200"
Port 7777

jdMorgan

2:11 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The %{HTTPS} is not a native Apache server variable, and sometimes it won't work if the mod_SSL module has not executed before this variable is tested. A better alternative in this case might be to use

RewriteCond %{SERVER_PORT} !^443$

since the scope of that variable is the HTTP request itself.

Jim

jdMorgan

2:16 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see that you're using port 7777 instead port 443. This might cause additional problems in other areas.

But yet anothe apternative might be to look at the 'tail' of %{THE_REQUEST} to check the request protocol:


RewriteCond %{THE-REQUEST} !HTTPS/[0-9]\.[0-9]+$

I am *not* sure that will work, but it may be worth a try...

Jim

dzi57

3:01 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I tried the RewriteCond, but it's still the same. when looking to rewrite_log there is no entry. How can check if rewrite is running properly?

Didier

jdMorgan

5:51 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something simple, like

RewriteRule ^foo\.html$ http://www.google.com [R=302,L]

Request /foo.html from your server, and you should land at google.

Note that as documented, mod_rewrite requires either Options FollowSymLinks or Options SymLinksIfOwnerMatch to be set, otherwise it won't run.

Jim

dzi57

1:36 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I like it when it is simple!
I can now made some tests on a non production server.
Rewrite rule is ok with your syntax when usin html or https.

It is not Ok when usin http with 443 port.
URL [mydomain...] => Ok
URL [mydomain...] => Ok
URL [mydomain:443...] => Nok with the following error:
[Thu Oct 02 15:50:12 2008] [info] [client 129.181.225.6] SSL handshake failed: HTTP spoken on HTTPS port; trying to send HTML error page
[Thu Oct 02 15:50:12 2008] [info] SSL Library Error: 336027804 error:1407609C:SSL routines:SSL23_GET_CLIENT_HELLO:http request speaking HTTP to HTTPS port!?

Didier

jdMorgan

1:54 pm on Oct 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, basically, you cannot do that -- request an https page using http, that is. The server won't even allow the connection. The simple test was intended only to establish whether mod_rewrite was working on your server.

You may now proceed to debug the specific coding issues, having established that mod_rewrite is installed and working.

Jim

dzi57

2:06 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



Jim,

1. Now it is established that mod_rewrite is installed and working.
2. You said that I cannot request an https page using http => Ok
3. By debuging the specific coding issues you mean it is possible to
do it with mod_rewrite?

Thanks again for your time

Didier

jdMorgan

2:27 pm on Oct 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure that you code is located in the correct <VirtualHost> container on the correct machine. Other than that, I've given all the advice I have on this issue, unless you find something specifically wrong and can post further specific questions (It is is almost impossible to debug a server remotely through a forum, especially with complex issues such as SSL and reverse-proxies). :)

Jim

dzi57

2:57 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



Hi Jim,

On my opinion there are no solutions for this issue.I have made several tests with always the same result: "SSL handshake failed: HTTP spoken on HTTPS port"
An http request on port 443 (ie:7777) go straight to the Virtual host configure for port 443 (ie:7777). No redirection is possible at this state..
Thanks again

Didier