How do I exclude a single directory from HTTPS to HTTP 301 redirect with IIS6+IIRF - iis-6

I already have a 301 from HTTPS to HTTP sitewide.
But I want to exclude checkout pages so that they are always HTTPS (located in "/shopping-cart/")
Tried this:
#RewriteCond %{SERVER_PORT} ^443$
#RewriteRule ^(.*)$ http://www.example.com$1 [R=301]
#RewriteCond %{SERVER_PORT} ^80$
#RewriteCond %{HTTP_HOST} ^www.example.com$
#RedirectRule ^/shopping-cart(.*)$ https://%{HTTP_HOST}/shopping-cart$1 [R=301]
This gives me a redirect loop when I get to /shopping-cart/ pages

What I think is causing the loop is the sequence of the rules. Swap them and add the [l] tag to the shopping_cart rule (the [l] means "last" i.e. stop processing further rules if this rule matches)
#RewriteCond %{SERVER_PORT} ^80$
#RewriteCond %{HTTP_HOST} ^www.example.com$
#RedirectRule ^/shopping-cart(.*)$ https://%{HTTP_HOST}/shopping-cart$1 [R=301, l]
#RewriteCond %{SERVER_PORT} ^443$
#RewriteRule ^(.*)$ http://www.example.com$1 [R=301]

Related

Redirect 301 appending old URL with New URL

I am having a problem while redirecting a page whose URL was changed but I want old URL to be redirected to new. Here is .htaccess file
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Using mod_rewrite in the last line I am not getting redirected to new URL but when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended to in it in this format.
https://example.com/contact-us/?/contactus/
I have searched Stack Overflow for solution to this problem and tried many similar scenarios but that didn't work.
Have it like this the given order.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^contactus/$ /contact-us/ [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/system [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On is not required multiple times in same .htaccess.
Make sure to clear your browser cache before testing the change.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Your mod_rewrite external redirects (the last three rules) are in the wrong place. These need to be before the internal rewrite (front-controller). By placing them at the end of the file they are never going to get processed for any URL other than static resources, since all other URLs are routed to index.php, at which point processing stops.
when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended
Different modules are processed independently regardless of the order of the directives in the config file. mod_alias is processed after mod_rewrite - but it is processed. However, the Redirect directive is prefix matching and everything after the match is appended onto the end of the target URL.
And there's no need to repeat the RewriteEngine directives
Have your directives like this:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/system
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
And make sure to clear your browser cache before testing. Preferably test with 302 (temporary) redirect to avoid caching issues.

.htaccess rewrite for https and hidden subfolder

I need to rewrite http://www.example.com and http://example.com to
https://www.example.com.
An additional complexity is that these URLs are not pointing to the index file, but to the /folder. i.e. https://www.example.com really points to /var/www/html/folder, without showing https://www.example.com/folder in the URL bar.
I had previously managed to attain the second part with
RewriteCond %{HTTP_HOST} www.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ folder/$1 [L]
and I know that the first part can be attained with something like this:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
but somehow I just can't get both of them to work together because I'm not familiar with htaccess. Thanks in advance!
You can have a single rule to add www and http -> https redirection and then have your rewrite rule for folder forward:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!folder/)(.*)$ folder/$1 [L,NC]

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Force SSL on your website using .htaccess and Forcing the "www." at the beginning of URLs

I was using this code and it successfully forces ssl:
RewriteEngine On
RewriteCond %{HTTP_HOST} www\.example\.com
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
however it does not force www at the begining of urls. I got this supposed solution posted on another page here:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
But what i get when i attempt to open my page is this:"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
How may i fix this, that is, not only force ssl but also force www at the beginning of urls?
Thanks.
Try:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

mod_rewrite on substring in URL

I'm forcing HTTPS on my site using mod_rewrite but I want to change this to HTTP for any URL with the substring com_bookmangement in the URL.
So
http://www.example.com/index.php?option=com_content&view=article&id=85&Itemid=140
will be directed to
https://www.example.com/index.php??option=com_content&view=article&id=85&Itemid=140
BUT
https://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216
will be directed to
http://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216
I've tried this without success:
#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} !com_bookmanagement=
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#rewrite vouchers views to http
#RewriteCond %{server_port} 443
RewriteCond %{REQUEST_URI} ^com_bookmanagement=
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
Any ideas?
If you use ^com_bookmanagement= it will only match if com_bookmanagement= appears at the start of the string. Try it without the ^ and =. Or what I would use:
#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{QUERY_STRING} !(^|&)option=com_bookmanagement(&|$)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#rewrite vouchers views to http
RewriteCond %{HTTPS} !=off
RewriteCond %{QUERY_STRING} (^|&)option=com_bookmanagement(&|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Condition on QUERY_STRING instead of REQUEST_URI:
RewriteCond %{SERVER_PORT} !443
RewriteCond %{QUERY_STRING} !com_bookmanagement
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} com_bookmanagement
RewriteRule ^/(.*)$ http://www.mysite.com/$1 [R=301,L]
More on rewriting URIs with query strings here.