SaranacLake Posted January 29, 2021 Share Posted January 29, 2021 Hello. I am trying to streamline a mod_rewrite that I have. Generically, I will have 3 RewriteCond lines. Logically, I want this... (RewriteCond1 AND RewriteCond2) OR RewriteCond3 In essence, I need Apache to see parentheses around the 1st and 2nd RewriteCond and then take the results of that and OR it with the 3rd RewriteCond. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/ Share on other sites More sharing options...
requinix Posted January 29, 2021 Share Posted January 29, 2021 It's not possible to do that. What are the conditions? There might be a way to alter them to work without "parentheses". Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584108 Share on other sites More sharing options...
SaranacLake Posted January 29, 2021 Author Share Posted January 29, 2021 (edited) 7 hours ago, requinix said: It's not possible to do that. What are the conditions? There might be a way to alter them to work without "parentheses". This is regarding an old thread where I was having issues with rewriting from non-www and www to https://www and ClouFlare. This was the code you helped me come to... RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^https://www.domain.com%{REQUEST_URI} [L,R=301] As I recall, without CloudFlare, that worked fine. But with CloudFlare turned on, my webhost suggested this... #RewriteCond %{HTTP_HOST} !^www\. #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] #RewriteCond %{HTTPS} off #RewriteCond %{HTTP:X-Forwarded-Proto} !https #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] And I was hoping for something cleaner and without an unnecessary redirect like maybe this... (Pseudo-code added for clarity) *IF* #RewriteCond %{HTTPS} off *AND* #RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] #RewriteCond %{HTTP_HOST} !^www\. *THEN* RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] This is for a new server, so all of this is untested, but I hope to get a new CloudFlare account for this new server today, and try to get everything working, all with having a well-written, and efficient mod_rewrite! Edited January 29, 2021 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584112 Share on other sites More sharing options...
requinix Posted January 29, 2021 Share Posted January 29, 2021 If that's exactly what your host suggested then it's wrong. If the problem is the unnecessary redirect, what if I told you there is only one redirect (if done correctly)? RewriteCond %{HTTP_HOST} !=www.domain.com RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [R=301,L] RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] That's slightly modified from other versions because today is an odd-numbered weekday. Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584127 Share on other sites More sharing options...
SaranacLake Posted January 29, 2021 Author Share Posted January 29, 2021 (edited) 2 hours ago, requinix said: If that's exactly what your host suggested then it's wrong. How so? (Hopefully you didn't take my *IF*, *AND*, *THEN* literally...) Quote If the problem is the unnecessary redirect, what if I told you there is only one redirect (if done correctly)? I was trying to do things with a single RewriteRule, although upon reflection, I guess having two RewriteRule statements doesn't imply two redirects... Quote RewriteCond %{HTTP_HOST} !=www.domain.com RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [R=301,L] RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] That's slightly modified from other versions because today is an odd-numbered weekday. But that is basically what I have now.... I was hoping to get this to work... RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] That way there is only one RewriteRule. Can i make the above code behave the way I implied earlier? or must I break things up into two blocks each having its own RewriteRule? Edited January 29, 2021 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584137 Share on other sites More sharing options...
requinix Posted January 29, 2021 Share Posted January 29, 2021 34 minutes ago, SaranacLake said: How so? The non-www one does an infinite redirect. 34 minutes ago, SaranacLake said: But that is basically what I have now.... Except it works, yes. 34 minutes ago, SaranacLake said: I was hoping to get this to work... Like I said, there is no way for mod_rewrite to do (cond AND cond) OR cond. There is a way to do it all in one single RewriteRule, but it means more processing overhead. Besides, having two RedirectRules for two different reasons makes sense: one of them handles non-www domains, one of them handles https. Yeah, it would be nice to combine both, but it doesn't actually matter. Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584138 Share on other sites More sharing options...
SaranacLake Posted January 29, 2021 Author Share Posted January 29, 2021 4 minutes ago, requinix said: The non-www one does an infinite redirect. Except it works, yes. Sorry if I am missing something, but isn't this code - written as two separate blocks - the same as yours? RewriteCond %{HTTPS} off #RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] Otherwise, how is this code causing an "infinite redirect"? I just switched the order and check for the HTTPS off first sicne that covers 4 of the 5 scenarios which I want to rewrite... 4 minutes ago, requinix said: Like I said, there is no way for mod_rewrite to do (cond AND cond) OR cond. There is a way to do it all in one single RewriteRule, but it means more processing overhead. Besides, having two RedirectRules for two different reasons makes sense: one of them handles non-www domains, one of them handles https. Yeah, it would be nice to combine both, but it doesn't actually matter. I guess it was just wanting prettier code. Btw, does it matter if I have [L,R=301] versus [R=301,L] ?? Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584139 Share on other sites More sharing options...
requinix Posted January 29, 2021 Share Posted January 29, 2021 12 minutes ago, SaranacLake said: Sorry if I am missing something, but isn't this code - written as two separate blocks - the same as yours? Basically, yes, but that's not what you posted. You posted #RewriteCond %{HTTP_HOST} !^www\. #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] I can assume that the # commenting was added by you and not what the host said to do, but the part where it checks if the domain is wrong and, if so, redirects to the same domain is less likely to be. 14 minutes ago, SaranacLake said: Btw, does it matter if I have [L,R=301] versus [R=301,L] ?? I don't think so. In fact I'm not even sure [L] matters if you use [R]. Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584140 Share on other sites More sharing options...
SaranacLake Posted January 29, 2021 Author Share Posted January 29, 2021 3 minutes ago, requinix said: Basically, yes, but that's not what you posted. Okay, good. 3 minutes ago, requinix said: You posted #RewriteCond %{HTTP_HOST} !^www\. #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] I can assume that the # commenting was added by you and not what the host said to do, but the part where it checks if the domain is wrong and, if so, redirects to the same domain is less likely to be. Aha! My bad, I copied and pasted code from my .htaccess that I was playing around with things, and didn't notice that I had those lines commented out when I pasted here. My bad! 3 minutes ago, requinix said: I don't think so. In fact I'm not even sure [L] matters if you use [R]. Okay. I am setting up CloudFlare on my VPS now, so we'll see if this code works with CloudFlare - although I just have a dummy inex.php up, so i won't be able to test how it works with my work-in-progress site for a while, but I guess I can add some dummy folders and files and do a quick spot check. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/312070-rewritecond-and-or/#findComment-1584141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.