.htaccess url rewrite for ssl https redirection

2019-05-06 16:19发布

问题:

I've used different codes provided here on other questions' solutions and on the internet. I'm really not savvy with htaccess. Bought and confirmed working SSL Certificate, but I'm new to applying these redirects.

Goal: I need to rewrite http to https on the following directories.

  • http://mydomain.com/products-page/checkout
  • http://mydomain.com/products-page/your-account
  • http://mydomain.com/wp-login

I'm on shared hosting via Dreamhost. I have a dedicated IP, if that helps.

Initial code I was using recommended to me by a Dreamhost representative:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} wp-login 
RewriteRule ^(.*)$ https://mydomain.com/wp-login/$1 [R,L]

回答1:

Try these rules in the htaccess file in your document root.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/wp-login|/products-page/checkout|/products-page/your-account)
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,L]

The first condition checks if the request isn't HTTPS, the second checks if the request starts with either /wp-login, /products-page/checkout, or /products-page/your-account, and if both apply, then the rewrite simply takes the entire URI and redirects to https://.