2011-01-26 64 views
0

好吧,我有我的.htaccess这个重写规则:的.htaccess HTTPS重定向,同时保持网址简短

RewriteRule ^settings https://%{HTTP_HOST}/user_settings.php 

这种重定向“http://domain.com/settings”为“https://域名.COM/user_settings.php”。

我该如何使它重定向“https://domain.com/settings”到“https://domain.com/settings”?这意味着,我怎么能重定向从HTTP HTTP到HTTPS的网址,同时仍然保持重定向短网址在.htcaccess?

谢谢!

回答

1

编辑:问题澄清后更新答案。

你可以做这样的事情:

RewriteCond %{HTTPS} !=on 
RewriteRule ^settings(/?)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,NC] 
RewriteCond %{HTTPS} =on 
RewriteRule ^settings(/?)$ /user_settings.php [L,NC] 

第一RewriteCond将检查HTTPS是关闭的,如果是以下RewriteRule才会发生。

第一RewriteRule将匹配下列之一:

  • http://domain.com/settings
  • http://domain.com/settings/

(/?)允许尾随正斜杠是可选的。在[R,NC]中的R强制这是重定向,NC意味着它不区分大小写。

第二个RewriteCond将再次检查HTTPS,但这次确保它已打开。

如果HTTPS已打开,并且您位于您的目录(即第一次重写成功),它将重写为user_settings.php[L,NC]中的L表示如果该规则匹配,则应该遵循最后的RewriteRule

+0

但是它如何知道使用user_settings.php作为重定向?这只会给404错误。 – user591225 2011-01-26 21:40:56