0

I've got an HTACCESS which contols HTTPS/HTTP redirection based on url, most of it is easy to do except for a specific part.

The basket is dual protocol, some parts of it is HTTP and others is HTTPS namely the part where you enter your shipping and billing address and the part where you enter your credit card information. But the rest, such as /basket/, /basket/delete/, /basket/add/, /basket/modify/ should be HTTP.

I was able to do the the HTTP => HTTPS part but the reverse is quite harder because RewriteCond seems to be an AND, and i need OR...

At the same time, if you guys can help me make it more compact and readable, i'd be really happy, mod_rewrite is not my best tool :)

Right now it looks like this: (Note i know some rules seem to repeat but we have issues with the urls, so i need 2 versions most of the time for the basket part)

Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^admin/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !basket/$
RewriteCond %{REQUEST_URI} !basket/add/
RewriteCond %{REQUEST_URI} !basket/delete/
RewriteCond %{REQUEST_URI} !basket/modify/
RewriteCond %{REQUEST_URI} !basket//add/
RewriteCond %{REQUEST_URI} !basket//delete/
RewriteCond %{REQUEST_URI} !basket//modify/
RewriteCond %{REQUEST_URI} !basket/info_panier.js
RewriteRule ^basket/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !panier/$
RewriteCond %{REQUEST_URI} !panier/add/
RewriteCond %{REQUEST_URI} !panier/delete/
RewriteCond %{REQUEST_URI} !panier/modify/
RewriteCond %{REQUEST_URI} !panier//add/
RewriteCond %{REQUEST_URI} !panier//delete/
RewriteCond %{REQUEST_URI} !panier//modify/
RewriteCond %{REQUEST_URI} !panier/info_panier.js
RewriteRule ^panier/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^utilisateurs/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^users/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^produits/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^products/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteRule ^produits/ /catalogue_files/default/fr.php [L]
RewriteRule ^products/ /catalogue_files/default/en.php [L]

RewriteRule ^panier/ /panier_files/default/fr.php [L]
RewriteRule ^basket/ /panier_files/default/en.php [L]

RewriteRule ^utilisateurs/ /utilisateurs_files/default/fr.php [L]
RewriteRule ^users/ /utilisateurs_files/default/en.php [L]

</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I was thinking of adding something like this:

RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !admin/
RewriteCond %{REQUEST_URI} !users/
RewriteCond %{REQUEST_URI} !utilisateurs/
RewriteCond %{REQUEST_URI} basket/$
RewriteCond %{REQUEST_URI} basket/delete/
RewriteCond %{REQUEST_URI} basket/modify/
RewriteCond %{REQUEST_URI} basket//add/
RewriteCond %{REQUEST_URI} basket//delete/
RewriteCond %{REQUEST_URI} basket//modify/
RewriteCond %{REQUEST_URI} basket/info_panier.js
RewriteCond %{REQUEST_URI} produits/
RewriteCond %{REQUEST_URI} products/
RewriteCond %{REQUEST_URI} pdv/
RewriteCond %{REQUEST_URI} pos/
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

But this script will obviously will not work because RewriteCond is based on AND and not OR. So you can never be in basket/$ and basket/delete/ for example. So i'm stumped as to how to do this...

Thanks

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71

1 Answers1

1

First: with RewriteCond you can use [OR] directives to reach you goal.

But you can write much shorter RewriteCond:


Your last request should be like this:

RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !(admin|users|utilisateurs)/
RewriteCond %{REQUEST_URI} ^(((panier|basket)[/]{0,2}(add/|delete/|modify/|info_panier.js)?)|produits/|products/|pdv/|pos/)$
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)


Optimization

Your whole rules for the port 80 should be very close to:

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !((panier|basket)/(/)?(add/|delete/|modify/|info_panier.js))
RewriteRule ^(admin|basket|panier|utilisateurs|users)/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Your rules for the port 443 should be very close to:

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(produits|products)/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • OliverPons has always a `mod_rewrite` solution :) – vdegenne Nov 14 '11 at 18:09
  • @Oddant If you ever read my rewriterules for my framework you'll be scared to death... 100 lines of rewriterules and 400 lines of comment ;) – Olivier Pons Nov 14 '11 at 20:30
  • I can't even imagine.. considering you're able to mix dozens of them in three lines.. any git where I can see that ? (I assume it also is for the benefit of the question : training with the mod_rewrite module) – vdegenne Nov 14 '11 at 20:40
  • I've shown them here: http://stackoverflow.com/questions/7702667/php-handling-vs-apache-rewriterules-and-regexp-which-one-is-quicker but without the 400 lines of comments (and with a huge search replace to hide what I'm actually doing)... – Olivier Pons Nov 14 '11 at 20:52
  • ! That gave me instant heartburns, thanks I'll be training reading that for hours :) – vdegenne Nov 14 '11 at 21:18
  • You were looking for multisite + multilanguage framework... I've written one which is heavily based on Apache rewrite rules (after the 4th full rewrite...) if you want more information take a look at some of the answers I've given there's one (can't find it) where I explain how to do it. – Olivier Pons Nov 14 '11 at 21:24
  • I've been staring your post cause the question was interesting. But I really crave to see this framework in use. Really ? no webhost where I can see how your framework works ? (cause I've been coding a little cms for my own purpose) so it would be great to share experiences outta Stackoverflow – vdegenne Nov 14 '11 at 21:38
  • Nice, thanks, i'll look into this asap and if it works i'll mark as answer. Looks very nice so far – Mathieu Dumoulin Nov 15 '11 at 16:06
  • My pleasure @Oddant: I'm about to release the first site I've done *on my own* from scratch (= 4th rewrite of my framework) with the whole rewriterules. You'll see how fast it is, how clean it is by the end of this week. My framework is hard to understand if you want to understand what's going on "under the hood" but it's very, very powerful. – Olivier Pons Nov 15 '11 at 18:08