redirect to https url with get parameter - apache

I have .htaccess file and code gives only https on pages of woocommerce shop and works perfectly. So how can I add rule that let https on page with GET parameter 'yandex_money=check' for example https://example.com/?yandex_money=check
RewriteEngine On
# Force HTTPS for /(cart|checkout|my-account|product-category|shop|product)
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/(cart|checkout|my-account|product-category|shop|product) [NC]
RewriteRule ^(cart|checkout|my-account|product-category|shop|product) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTP for anything which isn't /(cart|checkout|my-account|product-category|shop|product)
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/(cart|checkout|my-account|product-category|shop|product) [NC]
RewriteRule !^(cart|checkout|my-account|product-category|shop|product) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

So the solution is
adding RewriteCond about %{REQUEST_URI} with [OR]
replace RewriteRule [L] with RewriteRule [N] and RewriteRule [L].
[N] key allows to check Next RewriteRule
[L] last is RewriteRule.
fixed .htaccess file:
RewriteEngine On
# RewriteCond %{HTTPS} on
# RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force HTTPS for /(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login) GET[yandex_money]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login) [OR,NC]
#RewriteCond %{QUERY_STRING} ^yandex_money=check [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login) https://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}[NC,R=301,N]
RewriteRule ^(yandex_money) https://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}[NC,R=301,L]
# Force HTTP for anything which isn't /(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login)
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login) [NC]
RewriteCond %{QUERY_STRING} !^yandex_money=check [NC]
RewriteRule !^(cart|checkout|my-account|product-category|shop|product|wp-admin|wp-login) http://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}

Related

How to disable https redirect on a single page with htaccess

I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section.
I have try below htacess code
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/page/* [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But still get stuck and it redirecting to https please any one?
I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section:
You may use this .htaccess code:
RewriteEngine On
# add www if missing to all the URIs
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# turn on https except for a specific URI
RewriteCond %{HTTPS} !on
RewriteCond %{THE_REQUEST} !/page[/\s?] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# force http for a specific page:
RewriteCond %{HTTPS} on
RewriteRule ^page(/|$) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,NC]

redirect all versions of URL to https://www

my problem is that I need all versions of my URL to redirect to https://www but I cannot figure it out.
i've tried this:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
but when i test it as:
example.com/hello
i get
https://example.com/hello
It does not add the www. Any thoughts on why?
Go ahead and try this.
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Should work for ya.

Force HTTPS for only a couple URLs?

I have tried every single thing I've come across to get only 2 specific URLs to redirect to https. NOTHING has worked. Here's what I have right now in .htaccess
RewriteCond %{REQUEST_FILENAME} topdeal(.*)
RewriteCond %{REQUEST_FILENAME} watchstore(.*)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try this rule with THE_REQUEST variable instead of REQUEST_FILENAME:
RewriteCond %{THE_REQUEST} watchstore|topdeal [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
You can use the following in your .htaccess file. What the below does is force everything to HTTP except for the directories watchstore and topdeal. Both are forced to HTTPs.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Make sure you clear your cache before testing this.
EDIT:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Using multiple URLs inside htaccess RewriteCond

I'm using the following code inside my .htaccess file to prevent certain URL from being served from https. How can I modify this code to include additional URLs? For example, I don't want the about page to load from https either. Adding an extra line similar to the careers link didn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
# Go to https if not on careers
RewriteCond %{SERVER_PORT} =80
RewriteCond %{THE_REQUEST} !/careers/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} !/capital/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} !/summer/[\s?] [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Go to http if you are on careers
RewriteCond %{SERVER_PORT} !=80
RewriteCond %{THE_REQUEST} /careers/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} /capital/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} /summer/[\s?] [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
</IfModule>
Better use regex alternation and minimize your code:
<IfModule mod_rewrite.c>
RewriteEngine On
# Go to https if not on careers
RewriteCond %{SERVER_PORT} =80
RewriteCond %{THE_REQUEST} !/(careers|capital|summer)/[\s?] [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Go to http if you are on careers
RewriteCond %{SERVER_PORT} !=80
RewriteCond %{THE_REQUEST} /(careers|capital|summer)/[\s?] [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
</IfModule>

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.