Forum Moderators: phranque

Message Too Old, No Replies

https to http redirect confusion

Redirect help?

         

oakc123

10:01 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Hello all,
I am trying to do a redirect from https to http for a sub domain.

For my main www domain I have
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [mydomain.com...] [L,R]

I have created a wiki.mydomain.com and I am doing this to redirect:

RewriteRule "^/WEB-INF/?(.*)" "$0" [L,F,NC]
RewriteRule "^/$" [localhost:8888...] [P]
RewriteRule "^/(.*)" "http://localhost:8888/$1" [P]
ProxyRequests Off
ProxyPassReverse / [localhost:8888...]
ProxyPreserveHost On

This all works except if I go to [wiki.mydomain.com...] I get my main secure site. I tried this above my redirect to localhost redirect:
#RewriteCond %{SERVER_PORT} ^80$
#RewriteRule ^/(.*) [wiki.mydomain.com...] [L,R]

And that does not work either. Can anyone suggest how I can fix this issue?

g1smd

10:20 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be careful here,
[R]
gives a 302 redirect. You'll likely need
[R=301,L]
instead.

You are using a leading slash on the pattern. That only works when the code is in httpd.conf; so you need to remove the slash when used in .htaccess; where is the code used?

oakc123

10:29 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



I am using the httpd.conf.

g1smd

10:52 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK. That clears up one potential issue.

I'm not all the conversant with Proxy stuff, so hang on a bit for someone else to have a go at tackling that stuff.

oakc123

10:59 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Thank you for the information thus far. To confirm I should change all [R]'s to [R=301, ...]. Can you direct me to more information on the redirect codes?

jdMorgan

2:20 am on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The redirect codes are documented in RFC2616 -- HTTP/1.1 Protocol, which you can find with a search. The basics are also covered in Apache's mod_rewrite document, which I assume you have a copy of.

The problem is that an https request that lands in your SSL host is NOT going to have a server port of 80; By definition it'll be 443. So, you need to check the requested hostname instead:


RewriteCond %{HTTP_HOST} ^wiki\.example\.com
RewriteRule ^/(.*) http://wiki.example.com/$1 [R=301,L]

Jim

[edited by: jdMorgan at 2:20 am (utc) on Jan. 13, 2009]

oakc123

6:31 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



Thank you for the information.

I added the following to my httpd.conf:
RewriteCond %{HTTP_HOST} ^wiki\.mydomain\.com
RewriteRule ^/(.*) [wiki.mydomain.com...] [R=301,L]

I get this error from firefox:
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing to accept cookies.

Here is the complete virtual host entry just in case:

<VirtualHost #*$!.#*$!.#*$!.#*$!:80>
ServerName wiki.mydomain.com
RewriteEngine On
#RewriteCond %{HTTP_HOST} ^wiki\.mydomain\.com
#RewriteRule ^/(.*) [wiki.mydomain.com...] [R,L]
RewriteRule "^/WEB-INF/?(.*)" "$0" [L,F,NC]
RewriteRule "^/$" [localhost:8888...] [P]
RewriteRule "^/(.*)" "http://localhost:8888/$1" [P]
ProxyRequests Off
ProxyPassReverse / [localhost:8888...]
ProxyPreserveHost On
ErrorLog /var/log/httpd/wiki-error_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
TransferLog /var/log/httpd/wiki-access_log
ServerAdmin webmaster@mydomain.com
ServerSignature On
</VirtualHost>

The only redirect I do for the main domain www is:

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [mydomain.com...] [L,R]

If needed I can include my complete Vhost for that domain as well.

jdMorgan

7:30 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to put the code I providided into the SSL server container. Otherwise, it obviously will redirect either http requests for the wiki.example.com domain to itself, creating a loop.

You have two variables that must be accounted for in the redirect/don't domain redirect decision, the hostname and the port number. Both of these have to be taken into account, either by the RewriteConds in the code itself, or by the containers (e.g. <VirtualHost> or <Directory> ) in which the code is placed.

So this wiki-to-http redirect code needs to go into the <VirtualHost w.x.y.z:443> container.

Jim

oakc123

10:56 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



So I am doing it incorrectly I have my Vhost for my main domain set to:

<VirtualHost w.x.y.z:80>
ServerName www.mydomain.com (side question can I have mydomain.com here as well)
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [mydomain.com...] [L,R]
RewriteRule ^/(.*) [mydomain.com...] [L,R]

...
</VirtualHost>

I only have the one vhost for port 80.

So from what I understand is that I should have 2 vhost one for 80 and one for 443.
<VirtualHost w.x.y.z:443>
ServerName www.mydomain.com
RewriteCond %{HTTP_HOST} ^wiki\.example\.com
RewriteRule ^/(.*) [wiki.example.com...] [R=301,L]
</VirtualHost>

And the final vhost (wiki) would be
<VirtualHost w.x.y.z:80>
ServerName wiki.mydomain.com
RewriteEngine On
--------------------
is this needed here
--------------------
#RewriteCond %{HTTP_HOST} ^wiki\.mydomain\.com
#RewriteRule ^/(.*) [wiki.mydomain.com...] [R,L]

--------------------
END
--------------------
RewriteRule "^/WEB-INF/?(.*)" "$0" [L,F,NC]
RewriteRule "^/$" [localhost:8888...] [P]
RewriteRule "^/(.*)" "http://localhost:8888/$1" [P]
ProxyRequests Off
ProxyPassReverse / [localhost:8888...]
ProxyPreserveHost On
ErrorLog /var/log/httpd/wiki-error_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
TransferLog /var/log/httpd/wiki-access_log
ServerAdmin webmaster@mydomain.com
ServerSignature On
</VirtualHost>

RewriteCond %{HTTP_HOST} ^wiki\.example\.com
RewriteRule ^/(.*) [wiki.example.com...] [R=301,L]

jdMorgan

5:34 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The wiki-to-http redirect code must be placed only into the <VirtualHost w.x.y.z:443> container.

Look at just these four lines. It should be clear that this code will always execute, because if the port is 80, then it will always be "NOT 443":


<VirtualHost w.x.y.z:[b]80[/b]>
ServerName www.mydomain.com
RewriteCond %{SERVER_PORT} [b]!^443$[/b]
RewriteRule ^/(.*) https://www.mydomain.com/$1 [L,R]

So clearly, any request to your server on port 80 (HTTP) will be redirected to HTTPS on port 443 by this code.

As for your question about "having 'mydomain.com' here as well," you can use the ServerAlias directive.

Jim

oakc123

8:44 pm on Jan 15, 2009 (gmt 0)

10+ Year Member



After finally understanding that I needed a container for wiki on port 443. I finally got it working. Thank you all for the information.

jdMorgan

8:45 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, I guessed right this time! :)

Glad you got it working...

Jim