Forum Moderators: phranque

Message Too Old, No Replies

Multiple Redirect issue

htaccess Multiple Redirect issue

         

bdmpastx

5:23 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



I am having a bit of an issue. I am trying to redirect 2 possible urls and I keep hitting a wall. This is all new to me but I am familiar with writing code, just not in this language.

I seem to be either creating a loop or something. Here is what I have.

The objective is to capture anyone wanting to go outside the SSL prtected zone (http) to (https) and anyone attempting to go to the non-www domain. So I researched and found several different ways to do this and I came up with this. The first rewrite works flawless as does the 3rd one. The middle one doesn't work.

So basically, if you type [mysite.com...] or [mysite.com,...] I want to send you to [mysite.com....] And I would like to catch any other variations that come after mysite.com like mysite.com/store or mysite.com/contact etc.

I was really hoping that the middle set of rewrite code would work. It looks like it would cover everything.

Here are my server details:
Server OS: Linux 2.6.22.10
Database: MySQL 5.0.75
HTTP Server: Apache/2.2.8 (Unix) FrontPage/5.0.2.2635
PHP Version: 5.2.6 (Zend: 2.2.0)




The code:

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ [mysite.com...] [L,R=301]

# RewriteEngine On
# RewriteCond %{HTTPS} !=on
# RewriteRule .* [%{HTTP_HOST}%{REQUEST_URI}...] [R,L]

# RewriteEngine On
# RewriteCond %{HTTPS} !=on
# RewriteCond %{HTTP_HOST} !^www\.
# RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]

jdMorgan

5:34 pm on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not just use a single rule and do both functions at once?

RewriteCond %{HTTP_HOST} !=www.example.com [OR]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Also, you only need "RewriteEngine on" once at the top of your rules.

Jim

bdmpastx

5:47 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



I get the IE cannot display page error using that code.

bdmpastx

7:21 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



I attempted to see the varibles passed from the server and it looks like when I use http and https, the port stays at 80. So I don't think the port 443 gets passed and therfore the code doesn't pass the query.

Anything else I can try?

jdMorgan

2:39 am on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hrrm... Odd set-up, that. I'd expect you may have problems using a non-standard port w/SSL.

Nevertheless, you can change the condition back to


RewriteCond %{HTTPS} !=on

I'm in the habit of swapping those out in my head as I type, because a good number of servers don't support the %{HTTPS} variable in the .htaccess context. So it's actually more common to have trouble with "%{HTTPS} !=on" than with "%{SERVER_PORT} !=443"

Ditch IE and use Firefox with the "Live HTTP Headers" add-on... Big productivity gain, and better "Alpha testing" value as well: That add-on lets you view all the headers passing back-and-forth between your server and your browser, and makes many redirection, protocol, port-selection, cache-control, authentication, and MIME-type problems a *lot* easier to spot. It's basic kit.

Jim

bdmpastx

5:41 am on Jan 9, 2010 (gmt 0)

10+ Year Member



I think I found the issue, I think my PHP config file was causing an infininte loop. I left the one redirect as it was and the HTTPS one is being handled in my PHP script. All is well.

When I used the RewriteCond %{HTTPS} !=on query, the loop would start as well as when the port 443. It must have conflicted with the PHP.