Forum Moderators: phranque

Message Too Old, No Replies

How can I redirect all pages to http except for certain pages?

         

engenius

7:16 am on Jul 8, 2009 (gmt 0)

10+ Year Member



I have looked through this forum and I have almost accomplished my goal but not quite.

I would like all pages to go to http except for a few direct links. I was able to do this but I would also like to redirect all https links to http if they are not one of the direct links that should be https.

I am also using the zend framework redirects and a redirect to make sure that the www is present. Here is my htaccess file:

==

#redirect https to http
#RewriteCond %{https} !=off
#RewriteRule ^/?(.*) http://www.example.org/$1 [R=301,L]

#redirect to www.example.org
RewriteCond %{HTTP_HOST} ^example.org$ [OR]
RewriteRule ^(.*)$ "http\:\/\/www\.example\.org\/$1" [R=301,L]

#redirect specific urls to https
RewriteCond %{https} !=on
RewriteCond %{REQUEST_URI} ^/parents/tuition [NC]
RewriteRule ^/?(.*) [example.org...] [R=301,L]

#zend framework
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

==

The file works without the first two lines except that it doesn't redirect https pages to http when they should not be secure. With the first two lines, I get an infinite loop.

I appreciate any help.

[edited by: jdMorgan at 12:47 pm (utc) on July 8, 2009]
[edit reason] example.org -- Please see TOS. [/edit]

jdMorgan

1:04 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your http->https and https->http redirects need to be fully complementary and 'symmetrical.' You've also got several errors and inconsistencies in syntax, incorrect rule order, and some inefficiencies. For use in .htaccess, I'd suggest:

# Externally redirect non-secure page requests from https to http
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^parents/tuition/
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
#
# Externally redirect /parents/tuition/ page requests from http to https
RewriteCond %{SERVER_PORT} !=443
RewriteCond $1 ^parents/tuition/
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
#
# Externally redirect all non-canonical hostname requests to www.example.org preserving http/https
RewriteCond %{HTTP_HOST} !^(www\.example\.org)?$
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.org/$1 [R=301,L]
#
#zend framework
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.php [L]

Important: You must replace the broken pipe "¦" character in the RewriteCond pattern above with a solid pipe character before use; Posting on this forum modifies the pipe character.

Jim

engenius

7:13 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



Thank you for the response.

Oddly, this works on my testing server (WAMP), but not on the actual server (Mac OSX Apache). Can you think of any difference that might cause a problem?

The part that doesn't work on the Mac server is redirecting /parents/tuition to https. Everything else works.

engenius

8:59 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I've been experimenting with the file and I think I figured out the problem but not the solution. I believe it has to do with this section.

# Externally redirect all non-canonical hostname requests to www.example.org preserving http/https
RewriteCond %{HTTP_HOST} !^(www\.example\.org)?$
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.org/$1 [R=301,L]

On my local server, the page gets redirected to [example.org...] but then stops there because I don't have a secure server setup.

If I physically type that address in a browser on the actual server, it ends up at http://www.example.org/index.php.

Typing in http://www.example.org/parents/tuiton just stays on http://www.example.org/parents/tuiton instead of being redirected to [example.org...]

It seems that this rule is overriding the previous rule and is always ending up at http.

jdMorgan

9:44 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I believe it has to do with this section.

No need to 'believe' -- comment out the code and prove or disprove it.

Did you replace the broken pipe as described above?
Do you have MultiViews or AcceptPathInfo enabled on the live server, but not on your test server?

Just a few tings to check...

Jim

engenius

5:37 am on Jul 9, 2009 (gmt 0)

10+ Year Member



I've played with this file for hours and it's still not working. I am about to lose my mind.

I think what you gave me works but there is some conflict with the URL rewriter for the Zend Framework. I will have to read up on that and see if I can't figure it out what is happening.

Thanks for your help.

jdMorgan

12:52 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be the same type of redirect+rewrite problem being discussed here: [webmasterworld.com...]

In that case, the following mod will fix it:


# Externally redirect non-secure page requests from https to http
RewriteCond %{SERVER_PORT} =443
[b]RewriteCond %{THE_REQUEST} !^[A-Z]+\ /parents/tuition/[/b]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
#
# Externally redirect /parents/tuition/ page requests from http to https
RewriteCond %{SERVER_PORT} !=443
[b]RewriteCond %{THE_REQUEST} ^[A-Z]+\ /parents/tuition/[/b]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
#
# Externally redirect all non-canonical hostname requests to www.example.org preserving http/https
RewriteCond %{HTTP_HOST} !^(www\.example\.org)?$
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.org/$1 [R=301,L]
#
#zend framework
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.php [L]

Completely flush your browser cache before testing any new server-side code.

Jim