.htaccess to redirect certain areas to https - regex

What I'm trying to do is serve the folder /soap OR pages that have /?p=uploads in title over http, the rest of the site over https.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)(p=uploads)(&|$) [NC,OR]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ - [E=IS_HTTP:1]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^soap/?(.*) http://www.example.com/soap/$1 [R,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/soap
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
It seems to work for the soap folder but not pages with /?p=uploads

Here is a way to do it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !soap/? [NC]
RewriteCond %{QUERY_STRING} !p=uploads/? [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,QSA]
All URLs are redirected to HTTPS, except those that have /soap or /?p=uploads

Related

.htaccess redirect to https with conditions page

I have the following .htaccess
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !(/webservice|/api)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(gif|jpg|png|css|js|swf)$ index.php [L]
I want all pages to be redirected to https, except /webservice and /api in those two I want it to open with both http and https.
In https requests everything works fine the problem is when I try to access http.
Problems
http://www.local.test/sub/pt-br/webservice redirect to https://www.local.test/index.php
http://www.local.test/sub/pt-br/api redirect to https://www.local.test/index.php
Any idea to make it work?
Change
RewriteCond %{REQUEST_URI} !(/webservice|/api)
to
RewriteCond %{REQUEST_URI} !^(\/webservice|\/api|\/sub\/pt-br\/webservice|\/sub\/pt-br\/api)
or
RewriteCond %{REQUEST_URI} !^\/webservice
RewriteCond %{REQUEST_URI} !^\/sub\/pt-br\/webservice
RewriteCond %{REQUEST_URI} !^\/api
RewriteCond %{REQUEST_URI} !^\/sub\/pt-br\/api

Redirect single page to HTTPS and the rest to HTTP

I'm trying to redirect a single WordPress page (/scheduling/) to HTTPS, while forcing HTTP on all of the other pages. There are a lot of similar posts on this topic, but my specific use case isn't working for some reason. Thoughts?
.htaccess
# 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
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^scheduling/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^scheduling/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The above is properly redirecting all other pages to be redirected to http whenever https is requested, but isn't doing anything at all for the scheduling/ page.
Do redirects before default WP rule.
Use THE_REQUEST variable instead of REQUEST_URI.
You can use:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /scheduling [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/scheduling [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

.htaccess HTTPS redirect except one page laravel

I want to redirect all HTTPS requests to HTTP except urls that contain this string:
"/account/buy-premium"
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/account/buy-premium [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/account/buy-premium [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller... Default Laravel conf
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
https:// example.com/test-page is redirecting to http:// example.com/test-page (good)
But
https://example.com/account/buy-premium/991 is redirecting to http://example.com/index.php (bad - no redirect is needed here)
I cannot find any solution to prevent redirect for the /buy-premium page
Please help
You will need to use THE_REQUEST variable instead of REQUEST_URI since REQUEST_URI changes to /index.php by your very last rule.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} \s/account/buy-premium [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !\s/account/buy-premium [NC]
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller... Default Laravel conf
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

.htaccess - 301 redirect move all pages to https:// - except 1 page?

I have the following code that redirects any http:// request to https:// - This work great, but how would I edit this to make an exception for one page e.g. mydomain.com/sitemap-news.xml - and keep this as http:// ?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As listed in comments here is how my entrie .htaccess looks
# 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
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
You can make an exception for sitemap file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

.htaccess redirect everything to https non-www and 404 to index

I was trying to use .htaccess to redirect any request to https non-www and every 404 to index (previous rules included). This is the end result, but it doesn't work as intended. What am I doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 https://mydomain.com/
You can use this code in your .htaccess:
ErrorDocument 404 https://mydomain.com/
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://mydomain.com%{REQUEST_URI} [L,R=301,NE]
This should work:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://mydomain.com/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [R=301,L]
The above will redirect all non-https to https and then if the file doesn't exist it is redirect to the index.