简体   繁体   中英

htaccess redirect to https all pages except one

I'm using Yii framework. I want to redirect all pages on the site from HTTP to HTTPS except this one domain.com/clip/create .

Following code redirects all pages to HTTPS:

RewriteEngine on
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

I modified that code to add exception for one page and it doesn't work properly. It works for all pages on the site, but on address domain.com/clip/create it redirects to https://domain.com/index.php My code is below:

RewriteEngine on

RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/clip/create
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/clip/create
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]    

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Any help is much appreciated.

Thanks

You want to remove the "https" part of the second redirect, (and you probably don't need the !-f and !-d checks either):

RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/clip/create
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]    
# no "s" here ---------^

It's probably because of your Yii controller. You'll need to prevent another redirect after the request is routed to the index.php controller. Add this to the very top (before the redirects):

RewriteRule index\.php - [L]

i'm terrible with htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/clip/create [OR]
RewriteRule ^/directory(.*)$ https://%{HTTP_HOST}/directory$1 [L,R]

got rules from here

clear your browser cache ! I faced many of these issues while I was working on switching connection type between HTTP and HTTPS , and then I found ,after a lot of searching and trying, that Firefox and chrome caches 301 and 302 error codes .

This will work for you:

RewriteEngine on

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} !/clip/create
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM