I want to redirect https requests to http for all but login and register pages.
So https:// example.com would redirect to http:// example.com but example.com/user/login should always redirect to https://example.com/user/login .
How do I do that in my .htaccess file?
You can detect if the current request uses https using the %{HTTPS}
variable in mod_rewrite. This is either on
or off
. Please note that the redirect you want will not have the intended behaviour for post requests, since the redirected url will always be requested using get without the post-body.
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/user/(login|register)$
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
RewriteCond %{HTTPS} =off
RewriteRule ^user/(login|register)$ https://example.com/user/$1 [R,L]
Change the R
flag to R=301
after testing the redirect works as expected.
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.