Forum Moderators: phranque

Message Too Old, No Replies

Rewrite http to https

.htaccess

         

hammerite

5:09 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



I have a kind of wierd one,

Ok rewriting http to https is normally pretty easy, but for some reason I cannot get this to work.

# redirect page to HTTPS should they be on the main site
# below line commented out as it is not needed in production yet, and
# affects testing.
##RewriteCond %{HTTP_HOST} ^(www\.)?some-domain\.com$ [NC]

### Following is due to server being behind load balancer
RewriteCond %{SERVER_PORT} 3187
RewriteCond %{HTTPS} off
RewriteRule ^some\-static\-page\.php$ [some-domain.com...]

Any help is appreciated.

jdMorgan

5:39 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is 3187 your SSL port, or your non-SSL port? And what is the other port number? -- We need to know both of them.

The %{HTTPS} variable is not a native Apache variable, and may not be defined at the time that this code runs. Therefore, it is better to avoid using it if you can base the conditional redirect on %{SERVER_PORT} only or instead of %{HTTPS}.

Jim

hammerite

5:52 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



3187 is non SSL, when the page is visited it should be redirected to another server which is listening on 443.

I think it may just be a simple matter of not interpreting the - properly

hammerite

6:45 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



I got it, kludgy but it works.

RewriteRule ^([a-z]+)-([a-z]+)-([a-z]+)-([a-z]+)-([a-z]+)-([a-z]+)\.php$ [some-domain.com%{REQUEST_URI}...] [NE
,R=301,L,QSA]

Unless you can think of a better way to do this. Thanks for the guidance!

jdMorgan

8:28 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know what your URLs look like, but at the very least you can improve that pattern:

1) You do not need parentheses, unless you:
a) Want to create back-reference -or-
b) Want to apply a quantifier to a group of characters or a subpattern instead of just the one that precedes the quantifier.

2) [NE] should not be required here, unless there is something going on that I can't see.
3) [QSA] is not required, unless you wish to *append* additional query string parameters to those in the original request.

So, for the exact same URL-path format that your pattern addresses, I'd use:


RewriteRule ^(([a-z]+-){5}[a-z]+\.php)$ https://www.some-domain.com/$1 [R=301,L]

Jim