Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

apache - .htaccess 301 redirect for all https to http EXCEPT ONE PAGE

Here is the code I have currently in my .htaccess file:

Options +FollowSymLinks 
RewriteEngine on
RewriteBase / 
RewriteCond %{HTTP_HOST} ^example.com [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I know jack about rewrites...and all of the other posts on this subject really don't fit my criteria and I don't know enough about the language to try and decipher it.

Basically what I need is this:

  1. redirect all instances of "example.com" to "www.example.com"
  2. redirect all instances of "https//www.example.com" to "http://www.example.com" with the exception of 1 page!!!! (in case it matters, the filename of that page is payments.php)

The code I have above works, but for the 1 page that I need to be https, it is rewriting the url to http. That one page has to be https.

Thanks, Chris

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apache/2.2.6 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8g PHP/5.2.6

I've tested it locally, all use cases seem to work fine. If you have further questions, feel free to ask.

# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /

# Redirect from example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} /payments.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !/payments.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

IMPORTANT! When the user navigates from any https page with www to any https page without www, he's asked to accept security certificate of your non-www domain.

For example (YES = request to accept the certificate, NO - opposite):

1) https://www.asdf.com/payments.php - YES (www.asdf.com)
2) http://www.asdf.com/phpinfo.php - NO
3) https://asdf.com/phpinfo.php - YES (asdf.com)
4) https://www.asdf.com/phpinfo.php - NO

I tried to reorder rules in .htaccess with no success. If anyone finds a better solution, it'll be highly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...