重写HTTP为HTTPS和WWW ISAPI重写



我已经拖了许多论坛并尝试了许多解决方案。没有一个正确的工作。我正在使用ISAPI重写3用于IIS。

我需要将所有请求更改为我们的网站www和https。

例如:

  • http s ://example.com/a-page-here/
  • http://example.com/a-page-here/
  • http://www.example.com/a-page-here/
  • www.example.com/a-page-here/
  • example.com/a-page-here/

所有更改为:

  • https://www.example.com/a-page-here/

我已经使用了http://htaccess.madewithlove.be,这可能是故障的,因为我在所谓的工作解决方案中似乎获得了不正确的结果。我不想在实时网站上测试不重要的事情。

这个据称正确的示例(我发现的众多)给出了错误的结果:

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]

示例测试:

  • example.com/a-page-here/ = https://www./example.com/a-page-here
  • www.example.com/a-page-here/ = https://www./www./www.example.com/a-page-here/

任何人都可以给我一组规则,这些规则可以清洁,可靠地将任何非www请求转到我们的网站上,以适用于正确的 https://www 版本,并且不添加无效的slashes等?

尝试这个:

RewriteEngine On 
# non-www to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC] 
RewriteRule (.*) https://www.example.com/$1 [R=301]
# HTTP to HTTPS 
RewriteCond %{HTTPS} off [NC] 
RewriteRule (.*) https://www.example.com/$1 [R=301] 

最新更新