.htaccess 301将所有https重定向到http除了一页

del*_*116 12 apache .htaccess rewrite url-rewriting

这是我目前在.htaccess文件中的代码:

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]
Run Code Online (Sandbox Code Playgroud)

我知道关于重写的杰克...而且关于这个主题的所有其他帖子确实不符合我的标准,我不太了解该语言来尝试和破译它.

基本上我需要的是这个:

  1. 将"example.com"的所有实例重定向到"www.example.com"
  2. 将" https //www.example.com"的所有实例重定向到" http://www.example.com " ,但1页除外!(如果重要,该页面的文件名是payments.php)

我上面的代码有效,但对于我需要https的1页,它将URL重写为http.那一页必须是https.

谢谢,克里斯

St.*_*and 10

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

我在本地进行了测试,所有用例似乎都运行良好.如果您还有其他问题,请随时提出.

# 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]
Run Code Online (Sandbox Code Playgroud)

重要!当用户从任何带有www的https页面导航到没有www的任何https页面时,他被要求接受非www域的安全证书.

例如(YES =请求接受证书,NO - 相反):

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
Run Code Online (Sandbox Code Playgroud)

我试图重新排序.htaccess中的规则但没有成功.如果有人找到更好的解决方案,我们将非常感激.


jsp*_*cal 5

除了威廉的伟大解决方案,你可以在https rewriterule之前否定请求uri

RewriteCond %{REQUEST_URI} !^/?payments\.php
Run Code Online (Sandbox Code Playgroud)