mod_rewrite .htaccess HTTPS

时间:2011-07-29 06:10:10

标签: mod-rewrite

如果有人输入,我想强制浏览器重定向到HTTPS:

http://www.mydomain.com/user
or
http://www.mydomain.com/user/

以上两个链接都应重定向到:https://www.mydomain.com/user

现在,第二行到底行的代码正常工作:

RewriteRule ^user$ https://www.mydomain.com/user/ [R,QSA]

确实会将浏览器重定向到HTTPS,但如果输入http://www.mydomain.com/user/,则它不会重定向到HTTPS。我怎样才能做到这一点?

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

RewriteRule ^user$ https://www.mydomain.com/user/ [R,QSA]
RewriteRule ^user/$ public/index.php?var1=x&var2=y [L,QSA]

2 个答案:

答案 0 :(得分:2)

使用以下规则:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# force www domain name
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

# force HTTPS for some pages
RewriteCond %{HTTPS} =off [NC]
RewriteRule ^user/?$ https://www.mydomain.com/user/ [R=301,L]

RewriteRule ^user/$ public/index.php?var1=x&var2=y [L,QSA]

它将重定向到HTTPS:

  • 如果HTTP是当前协议(如果已经在HTTPS上则没有重定向)
  • 无论尾随斜杠存在:/user/user/都会完成工作(但如果要求/user/something-here则不会执行任何操作)。

答案 1 :(得分:0)

这个怎么样......

RewriteRule ^user/(.+)/?$  public/index.php?var1=x&var2=y [L,R=301]
RewriteRule ^user/?$       https://www.mydomain.com/user/ [L,QSA]
RewriteRule ^(.*)/?$       http://www.mydomain.com/$1 [L,R=301]