Welcome to WebmasterWorld Guest from 100.26.176.182

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

.htaccess redirect http to https on one page only

redirect contact page one from http:// to https:// via .htaccess

     
6:52 pm on Nov 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 27, 2005
posts: 47
votes: 0


I recently added an SSL to my site and would like to 301 redirect http requests to my contact form to the secure, https URL.

I know this is wrong - anyone have the correct syntax (and the missing RewriteCond)?:

RewriteRule http://www.example.com/contact/ [secure.example.com...] [R=301,L]

[edited by: jdMorgan at 9:37 pm (utc) on Nov. 16, 2007]
[edit reason] example.com [/edit]

7:08 pm on Nov 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 27, 2005
posts: 47
votes: 0


does this look right?

RewriteCond %{SERVER_PORT}!^443$
RewriteCond $1 ^contact
RewriteRule (.*)$ [secure.example.com...] [R=301,L]

Seems to work...

[edited by: jdMorgan at 9:37 pm (utc) on Nov. 16, 2007]
[edit reason] example.com [/edit]

8:20 pm on Nov 16, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Simplify:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^contact https://secure.example.com/contact/ [R=301,L]

Note that as does your code, this code will redirect anything starting with "contact" using http to "contact/" using https. Note the "starting with" and the added trailing slash. I'm not sure if either of these factors was intentional. Without knowing the local URL-path of your contact form, I can't make a recommendation.

Jim

[edited by: jdMorgan at 8:20 pm (utc) on Nov. 16, 2007]

8:46 pm on Nov 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 27, 2005
posts: 47
votes: 0


Simplified version works perfectly - thanks Jim.

Note: I'm already handling the trailing slash issue (across all pages) with another rewrite.

Thanks again.

8:56 pm on Nov 16, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Point being that a request for http://example.com/contact/ will be redirected to [example.com...] by this code.

A proper solution might be:


RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^contact/?$ https://secure.example.com/contact/ [R=301,L]

This will redirect either
http://example.com/contact
-or-
http://example.com/contact/
to
[example.com...]

Jim

9:03 pm on Nov 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 27, 2005
posts: 47
votes: 0


Thanks Jim - even better.

All of the following requests are properly (301) redirecting:

http://example.com/contact
http://example.com/contact/
http://www.example.com/contact
http://www.example.com/contact/

to:

[secure.example.com...]

6:22 am on Nov 27, 2007 (gmt 0)

New User

10+ Year Member

joined:Nov 26, 2007
posts: 1
votes: 0


Following the code examples in this thread I have tried to go to and from http and https respectively AND to and from the domain without WWW and with WWW. However, I get caught in cycle of redirects. Here is my current .htaccess, where the first block to the secure protocol works fine... but the redirect is submitted to the second block where the 'not this URI' rule apparently is ineffective. Any advice would be much welcome.

...


RewriteEngine on

RewriteBase /

# Switch to https
RewriteCond %{HTTPS} off
RewriteRule ^7\+second\+opinion/$ [example.com.au...] [R=301,L]

# Switch back to http if https request
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/7\+second\+opinion/
RewriteRule (.*) http://www.example.com.au/$1 [R=301,L]

# Check that we are not in the images or scripts dir or on our main index.php processing file,
# then rewrite the "dir" after our base directory as a parameter into our main index.php file

#URI
RewriteCond %{REQUEST_URI} !common
RewriteCond %{REQUEST_URI} !includes
RewriteCond %{REQUEST_URI} !php
RewriteCond %{REQUEST_URI} !javascript
RewriteCond %{REQUEST_URI} !images
RewriteCond %{REQUEST_URI} !old
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !pdf
RewriteCond %{REQUEST_URI} !webalizer
RewriteCond %{REQUEST_URI} !video

#FILENAMES
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_FILENAME} !sitemap.xml
RewriteCond %{REQUEST_FILENAME} !main_styles.php
RewriteCond %{REQUEST_FILENAME} !javascript.js

RewriteRule ^(.+)$ index.php?dir=$1

#Rule to ensure that images work no matter which "dir" we are in
RewriteRule images/(.+)$ images/$1

php_flag register_globals on

AddType video/x-flv .flv
AddType application/x-shockwave-flash .swf

[edited by: jdMorgan at 2:43 pm (utc) on Nov. 27, 2007]
[edit reason] example.com [/edit]

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members