404 for link1? If link1 is actually index.php?key=link1, then we need to look closer at (and organize) your code (again).
RewriteEngine on
#force www [I]ON BOTH http AND https KEEPING THE PROTOCOL[/I]
RewriteCond %{HTTP_HOST} !www\\.site\\.com$ [NC]
RewriteRule .? http://www.site.com%{REQUEST_URI} [L,R=301]
#if traffic either http://www.site.com/link1 or http://www.site.com/link2, redirect to https. (and it works).
# [I]does not impact first block[/I]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(link1|link2|link3)$ https://www.site.com/$1 [R=301,L]
# [I]be sure to use correct regex, i.e., link1 is really index\\.php[/I]
#after one of those 2 pages is accessed, all the links go to https.
#if trafic is on https:// and the links are NOT https://www.site.com/link1 or https://www.site.com/link2, redirect to http://www. (and it does not work).
RewriteCond %{SERVER_PORT} !^80$
# start anchor and /'s removed as the referrer will include the domain
RewriteCond %{REQUEST_URI} !(link1|link2|/link3)$
RewriteRule .? http://www.site.com%{REQUEST_URI} [R=301,L]
# [I]WHERE DID THIS COME FROM???[/I]
# [I]The link1 block should be used after the force www to be handled properly BUT[/I]
# [I]the link1 references above are actual scripts, NOT representative of a key/value pairing![/I]
# [I]Same comment as for link1 but what is the key/value pair? How is link2 in the redirection to be handled?[/I]
# [I]link3/err? OMG! What is this? Where do you get var1 and var2 from?[/I]
# [I]link3 => index.php wiht another failed key/value pair? Again, where does var1 come from?[/I]
# None of the above make sense to me!
[COLOR="#FF0000"]RewriteEngine off[/COLOR]
# I think the fact that above links do not redirect to their form but to the index.php?key=... is somehow influenced by next redirects that don't trigger?
RewriteRule ^key/link1$ /link1 [L,R=301]
RewriteRule ^link1$ index.php?key=link1 [L]
RewriteRule ^link2$ index.php?link2 [L]
RewriteRule ^link3/err$ index.php?var1&var2 [NC,L]
RewriteRule ^link3$ index.php?var1 [NC,L]
When you create mod_rewrite code, you must do it logically and in an order in which any interaction between the different code blocks do not adversely impact the redirection (and the resultant file served). My term for the starting point is “SPECIFICITY,” putting your requirements in words. Doing that, you’ll be able to “hear” many of the looping problems or unintended redirections. In the above, I choose to deal with requests in the following order:
Enforce www (for your PR for http and for your SSL cert for https).
Enforce https for selected FILES, i.e., link1 you’ve noted is actually index.php. Remember, though, that the {QUERY_STRING} is unaffected by all this code as no new key/value pairs were created.
Of lesser importance, redirect files not necessary for the https section above to http protocol.
What you’ve tossed into the fray at the end, however, appears nonsensical (irrelevant to what we’ve accomplished thus far). If any of these are valid, you’ll need to insert them into the order BEFORE they are normally handled OR they will require additional RewriteCond statements to include their impact as required. Saving them to last means that your index.php must be one of your link# from block #2 and block #3.
Finally, I’m at a loss to figure out what you’re trying to do with valueless keys as well as where your var1 and var2 values are coming from so that needs to be explained, if you please.
Thank you again for your patience and willingness to help me!
The site structure is like this:
EVERYTHING is handled by index.php.
Also index.php uses several different keys/values (not just 1 key/value, but several : key1/value; key2/value… etc, and even empty keys).
That’s why there is a difference in the rules like index.php?key=value and index.php?var1.
The values keys tell index.php witch script to load (index.php includes different scripts based on the keys - the keys are all known and properly validated and sanitized).
Once the shopping basked is submitted it goes to step2 of the order process by method POST to -> /link2 (witch is in fact index.php?checkout).
If /link4 is somehow loaded without some necessary selections/validations from /link3 it errors and redirects to /link3/err witch in fact is index.php?var1&var2 (in this case var2 is misleading as it is not actually a variable but a static key that tells index.php?var1 that there is an error)
I know the way the site is proceeding data is not quite common and might be confusing, and I hope my explanations helped.
If using actual links to the site might help getting a better idea of what is happening I would be happy post the real links of the site.
Well, that makes sense IF the (linkx) keys are not allowed to be variable but, instead, a set like (cart|checkout|pay). What, then, are the varx values supposed to be?
Okay, I’m not trying to be “on your case,” only clarifying the many missing pieces of the “specificity” required to assist.
Well, now that that is clarified, let’s get back to business!
RewriteEngine on
#force www ON BOTH http AND https KEEPING THE PROTOCOL
RewriteCond %{HTTP_HOST} !www\\.site\\.com$ [NC]
RewriteRule .? http://www.site.com%{REQUEST_URI} [L,R=301]
#if traffic either http://www.site.com/link1 or http://www.site.com/link2, redirect to https. (and it works).
# does not impact first block
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(link1|link2|link3)$ https://www.site.com/$1 [R=301,L]
# be sure to use correct regex, i.e., link1 is really index\\.php
#after one of those 2 pages is accessed, all the links go to https.
#if trafic is on https:// and the links are NOT https://www.site.com/link1 or https://www.site.com/link2, redirect to http://www. (and it does not work).
RewriteCond %{SERVER_PORT} !^80$
# start anchor and /'s removed as the referrer will include the domain
RewriteCond %{REQUEST_URI} !(link1|link2|link3)$
RewriteRule .? http://www.site.com%{REQUEST_URI} [R=301,L]
# redirect to index.php with correct vars
RewriteRule ^key/link1$ /link1 [L,R=301]
RewriteRule ^link1$ index.php?key=link1 [L]
RewriteRule ^link2$ index.php?link2 [L]
RewriteRule ^link3/err$ index.php?var1&var2 [NC,L]
RewriteRule ^link3$ index.php?var1 [NC,L]
That should work … after you substitute your values for link1, link2, link3, key, var1 and var2. If not, please show your trial URI as well as your mod_rewrite code. You may PM me with that if you need to keep it private.