【问题标题】:htaccess, redirect to https for a specific file and to non-www httpshtaccess,针对特定文件重定向到 https 和非 www https
【发布时间】:2015-01-26 10:44:26
【问题描述】:

我有一个网站可以工作并尝试通过 htaccess 进行重定向几天。我已经在网上搜索并找到了很好的作品,尤其是在这个网站上,但是尽管我尝试了几乎所有的可能性,但我无法实现我想做的事情。

我的需要是将所有站点重定向到非 www http,包括 https,除了一个文件。假设重定向 http://www.example.com/.../....php?a=...
https://www.example.com/.. ./....php?a=...
https://example.com/.../....php?a=...

http://example.com/.../....php?a=...

但是,只有一个特定文件
http://www.example.com/.../theSpecificFile.php?a=...
https: //www.example.com/.../theSpecificFile.php?a=...
http://example.com/.../theSpecificFile.php?a=. ..
应重定向到
https://example.com/.../theSpecificFile.php?a=...

为此,我编写了许多 htaccess 文件,但在每种情况下,我都无法满足我的需求。例如在最后一个htaccess:

Options +FollowSymlinks        
RewriteEngine on    
RewriteBase /    

ErrorDocument 404 http://example.com/404.php    

#force https for certain pages        
RewriteCond %{HTTPS} !=on    
RewriteRule ^(theSpecificFile\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]    

RewriteCond %{HTTPS} on    
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]    

#redirect www.example.com to example.com (or any other subdomain)    
RewriteCond %{HTTP_HOST} !^example.com$ [NC]    
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] 

通过这个 htaccess 文件,

当我尝试 https://www 时,我得到 Unsecure or Untrusted connection(我正在从另一种语言翻译,希望它可能是真正的翻译)与 ssl_error_bad_cert_domain

当我尝试访问 theSpecificFile.php 时,我得到错误定义“infine loop”(我再次希望这可能是一个真实的翻译)。

这真的让我很沮丧,所以,任何帮助都将不胜感激。

提前致谢

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    看,在这里你将theSpecificFile.php 重定向到它的 https 版本,但是第二个重定向规则触发并将浏览器重定向回 http,然后从那里第一个规则重定向回 https...这就是你得到的原因无限循环。

    而 ssl_error_bad_cert_domain 表示证书不适用于此域,因此您可能需要检查是否使用了正确的证书。

    【讨论】:

    • 好的,在更改第一和第二行“RewriteCond 和 RewriteRule”的位置后,尝试进入 example.com 时,重定向到“example/com
    【解决方案2】:

    你可以使用它:

    RewriteCond %{HTTPS} on    
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{REQUEST_URI} !/theSpecificFile\.php [NC]
    RewriteRule ^ http://example.com/%{REQUEST_URI} [L,R=302]    
    
    RewriteCond %{HTTPS} off    
    RewriteCond %{REQUEST_URI} /theSpecificFile\.php [NC]
    RewriteRule ^ https://example.com/%{REQUEST_URI} [L,R=302]    
    

    当一切正常时将 [R=302,L] 更改为 [R=301,L]

    【讨论】:

      【解决方案3】:

      请尝试:

      RewriteCond %{HTTPS} on
      RewriteConf %{REQUEST_URI} !=/theSpecificFile.php
      RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
      

      【讨论】:

      • RewriteCond %{HTTPS} on RewriteConf %{REQUEST_URI} !=/theSpecificFile.php RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L] #force https for certain pages RewriteCond %{HTTPS} !=on RewriteRule ^(theSpecificFile\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R] 尝试使用 example.com 的 htaccess 并获取 https://example/com
      猜你喜欢
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2013-07-01
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      相关资源
      最近更新 更多