.htaccess重定向到https,一页除外

.htaccess重定向到https,一页除外,.htaccess,mod-rewrite,redirect,.htaccess,Mod Rewrite,Redirect,我想用.htaccess完成以下操作: 非www到www http到https,页面/my/smaple/page 如果文件名不是文件或目录,请执行index.php 目前情况是这样的: RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTPS} off RewriteCond %

我想用.htaccess完成以下操作:

  • 非www到www
  • http到https,页面
    /my/smaple/page
  • 如果文件名不是文件或目录,请执行index.php
  • 目前情况是这样的:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} !^/my/sample/page
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    如果我尝试访问
    http://www.mydomain.tld/my/smaple/page
    ,我将被重定向到
    https://www.mydomain.tld/index.php
    。不过,其他一切都是有效的。我的.htaccess知识非常少,请解释一下有什么问题。

    问题是,
    /My/sample/page
    被上一条规则困住,然后重写引擎循环。这就是正在发生的事情:

  • 请求发送至
    http://www.example.com/my/sample/page
  • 跳过第一条规则,
    %{HTTP\u HOST}
    以“www”开头
  • 跳过第二条规则,
    %{REQUEST_URI}
    /my/sample/page
  • 应用第三条规则,请求不针对现有文件名或目录。请求URI现在是
    /index.php
  • 重写引擎循环
  • 跳过第一条规则,
    %{HTTP\u HOST}
    仍以“www”开头
  • 第二条规则被应用
    %{REQUEST_URI}
    现在是“/index.php”,不是
    /my/sample/page
    ,请求被标记为重定向到
    https://www.exmaple.com/index.php
  • 重写引擎循环
  • 请求被标记为重定向,因此通过处理管道发送
  • 尝试将第二条规则更改为:

    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} !^/my/sample/page
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    


    这样,当重写引擎循环时,
    %{the_REQUEST}
    变量仍然是
    GET/my/sample/page
    ,因此条件仍然失败。

    我自己找到了解决方案。我这样工作很好

        RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} !/websocket_8087_check/
    RewriteCond %{THE_REQUEST} !/websocket_8087/
    RewriteCond %{THE_REQUEST} !/websocket_8086_check/
    RewriteCond %{THE_REQUEST} !/websocket/
    RewriteCond %{THE_REQUEST} !/websocket_8085_check/
    RewriteCond %{THE_REQUEST} !/websocket_8085/
    RewriteCond %{THE_REQUEST} !/websocket_8084_check/
    RewriteCond %{THE_REQUEST} !/websocket_8084/
    RewriteCond %{THE_REQUEST} !/websocket_8083_check/
    RewriteCond %{THE_REQUEST} !/websocket_8083/
    RewriteCond %{THE_REQUEST} !/websocket_8082_check/
    RewriteCond %{THE_REQUEST} !/websocket_8082/
    RewriteCond %{THE_REQUEST} !/websocket_8081_check/
    RewriteCond %{THE_REQUEST} !/websocket_8081/$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    有没有可能请求
    /my/sample/page
    映射到一个文件或目录?@Jon Li:
    /my/sample/page
    只是在php运行时从一个框架创建的一个路由,所以它肯定不存在。我在这个页面上使用了一些外部资源(主要是图像),这就是为什么我想使用http而不是https。我不认为最后一条规则会导致这个问题,因为它只是一个指向index.php的内部重定向。谢谢,工作非常完美。你能给我解释一下吗。重写引擎循环。我没想到会有一个循环,因为[L]@nipec重写引擎循环遍历所有规则,直到URI停止更改(或者如果有特定标志,如重定向或禁止)。
    L
    标志仅在当前迭代中停止重写引擎。因此,对于
    L
    ,它会在之后停止计算所有规则,但仍会循环。
        RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} !/websocket_8087_check/
    RewriteCond %{THE_REQUEST} !/websocket_8087/
    RewriteCond %{THE_REQUEST} !/websocket_8086_check/
    RewriteCond %{THE_REQUEST} !/websocket/
    RewriteCond %{THE_REQUEST} !/websocket_8085_check/
    RewriteCond %{THE_REQUEST} !/websocket_8085/
    RewriteCond %{THE_REQUEST} !/websocket_8084_check/
    RewriteCond %{THE_REQUEST} !/websocket_8084/
    RewriteCond %{THE_REQUEST} !/websocket_8083_check/
    RewriteCond %{THE_REQUEST} !/websocket_8083/
    RewriteCond %{THE_REQUEST} !/websocket_8082_check/
    RewriteCond %{THE_REQUEST} !/websocket_8082/
    RewriteCond %{THE_REQUEST} !/websocket_8081_check/
    RewriteCond %{THE_REQUEST} !/websocket_8081/$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]