问题
I have a wordpress site that I wanna change https to http for only '/wp-admin' and 'wp-login.php'. I wanna use .htaccess to achieve it, but I m not good at regular expressions. Below is my current htaccess codes.
# Force HTTPS for /my
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/wp-admin [NC]
RewriteRule !^wp-admin https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTP for anything which isn't /my
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/wp-admin [NC]
RewriteRule ^(wp-admin) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
Currently it works for wp-admin/, but not for /wp-login.php. How to modify this?
回答1:
Replace your Rules with this :
RewriteEngine on
#http to https except "/wp-admit" and "/wp-login.php"
RewriteCond %{HTTPS} !on
RewriteRule ^((?!wp-admin.*|wp-login\.php).*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#Redirect "/wp-admin" and "/wp-login.php" to http if accessed using https scheme
RewriteCond %{HTTPS} on
RewriteRule ^(wp-admin.*|wp-login\.php)$ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Clear your browser cache before testing these rules.
来源:https://stackoverflow.com/questions/43444143/how-to-redirect-site-to-https-and-http-using-htaccess-for-specific-file-and-fold