将单个页面重定向到HTTPS,其余部分重定向到HTTP

时间:2015-05-19 07:59:24

标签: regex apache .htaccess mod-rewrite

我尝试将单个WordPress页面(/scheduling/)重定向到HTTPS,同时在所有其他页面上强制HTTP。关于这个主题有很多类似的帖子,但我的具体用例由于某种原因不起作用。想法?

的.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

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

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

以上是在请求http时正确地重定向所有其他要重定向到https的网页,但是scheduling/页面根本没有做任何事情。

1 个答案:

答案 0 :(得分:1)

  1. 在默认WP规则之前重定向。
  2. 使用THE_REQUEST变量代替REQUEST_URI
  3. 您可以使用:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} /scheduling [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{THE_REQUEST} !/scheduling [NC]
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress