简体   繁体   中英

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. 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. Let's say redirect http ://www.example.com/.../....php?a=...
https ://www.example.com/.../....php?a=...
https ://example.com/.../....php?a=...
to
http ://example.com/.../....php?a=...

However, only one specific file
http ://www.example.com/.../theSpecificFile.php?a=...
https ://www.example.com/.../theSpecificFile.php?a=...
http ://example.com/.../theSpecificFile.php?a=...
should be redirected to
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. eg At the last 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,

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

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).

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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