简体   繁体   English

htaccess,重定向到特定文件的https和非www https

[英]htaccess, redirect to https for a specific file and to non-www https

I've a site to work and trying to make redirect by htaccess for few days. 我有一个网站可以工作,并尝试通过htaccess进行几天的重定向。 I've searched the net and found good works, especially in this site, but altough I've tried almost every possibilities, I've could not achieve what I want to do. 我已经在网上搜索并找到了不错的作品,尤其是在该网站上,但是尽管我尝试了几乎所有的可能性,但我仍未实现我想做的事情。

My need is redirect all site to non-www http, including https, except for only one file. 我需要将所有站点重定向到非www http,包括https,仅一个文件除外。 Let's say redirect http ://www.example.com/.../....php?a=... 假设重定向http://://www.example.com/.../....php?a = ...
https ://www.example.com/.../....php?a=... https://www.example.com/.../....php?a = ...
https ://example.com/.../....php?a=... https://example.com/.../....php?a = ...
to
http ://example.com/.../....php?a=... http://example.com/.../....php?a = ...

However, only one specific file 但是,只有一个特定文件
http ://www.example.com/.../theSpecificFile.php?a=... http://www.example.com/.../theSpecificFile.php?a = ...
https ://www.example.com/.../theSpecificFile.php?a=... https://www.example.com/.../theSpecificFile.php?a = ...
http ://example.com/.../theSpecificFile.php?a=... http://example.com/.../theSpecificFile.php?a = ...
should be redirected to 应该重定向到
https ://example.com/.../theSpecificFile.php?a=... https://example.com/.../theSpecificFile.php?a = ...

To do these, I've wrote many htaccess files but in each case, I couldn't achieve my needs. 为此,我编写了许多htaccess文件,但在每种情况下,我都无法满足我的需求。 eg At the last 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] 

By this htaccess file, 通过此htaccess文件,

when I try to https ://www I get Unsecure or Untrusted connection (I'm translating from another language, hope it might be true translation) with ssl_error_bad_cert_domain 当我尝试通过ssl_error_bad_cert_domain获得https://:// www的 不安全或不可信连接 (我正在从另一种语言翻译,希望它可能是真正的翻译)时

and when I try to access to the theSpecificFile.php I get error defining " infine loop " (again I hope this might be a true translation). 当我尝试访问theSpecificFile.php时,出现定义“ infine loop ”的错误(再次希望这可能是真正的翻译)。

This is really frustrating for me, so, any help would be highly appreciated. 这真的让我感到沮丧,因此,任何帮助将不胜感激。

Thanks in advance 提前致谢

Look, here you redirect theSpecificFile.php to its https version, but then the second redirect rule triggers and redirects the browser back to http, and from there the first rule redirects back to https... That's why you're getting the infinite loop. 看,在这里您将theSpecificFile.php重定向到其https版本,但是第二条重定向规则触发并将浏览器重定向到http,然后从那里将第一条规则重定向回到https ...这就是为什么会出现无限循环的原因。

And ssl_error_bad_cert_domain means the certificate isn't for this domain, so you might need to check if you're using the right one. ssl_error_bad_cert_domain表示该证书不适用于该域,因此您可能需要检查是否使用了正确的证书。

Please try: 请试试:

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

You can use that: 您可以使用:

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]    

Change [R=302,L] to [R=301,L] when everything works well 一切正常时,将[R = 302,L]更改为[R = 301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM