redirect to SSL with the exception of one URL

2019-08-02 03:44发布

问题:

I run a small bootstrap themes site that allows users to upload themes and templates.

I've been able to redirect any non SSL connection to https via my .htaccess file with the following:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I now need to redirect the user to non-SSL for one page in particular. It's a page containing an iframe that loads a preview from the theme authors site and these sites aren't always served via SSL.

The URL always has the word 'preview' preceded by the domin.

Here's is what I've tried, unfortunately without success:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on

RewriteCond %{REQUEST_URI} ^/preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]

Here are two examples:

non-preview link: http://www.bootstrapcovers.com/bootstrap-themes/all/free/sort.downloads/page.1

preview link http://www.bootstrapcovers.com/preview/1/adminlte-admin-control-panel

Any idea why it's not working or what I'm missing from the .htaccess file?

Thanks, -Paul

回答1:

Use THE_REQUEST instead of REQUEST_URI which might change due to other modules or rules:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# other rules go here

Also make sure to clear your browser cache.