Redirect all from http to https except contact page - apache

How do I redirect all pages on a site from https to http except the /contact page which I always want to be redirected to the https domain?
I can redirect all from http to https as follows:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
However, I'm having trouble adding the exception for the contact page which I want to always redirect from http to https
update
I've added the rules as suggested but /contact is now redirecting to /index.php?q=contact.
I'm using ModX which has the rules:
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Why is this conflicting with the new rule to redirect /contact (http) to /contact (https)?
Thanks :)

Use this:
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/contact\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/contact\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

You can try this rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/contact$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^contact$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can test your .htaccess file using this online tool (note that it doesn't includes all Apache functionalities and the result might slightly differ from your production server but works well for simple rules).

Try this:-
RewriteEngine On
RewriteBase /
//Turn https on for /page/contact
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page/contact
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
//Turn https off everthing but /page/contact
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/page/contact
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
The first rule set will make sure the page /page/contact will redirected back to https if they are accessed via http. The second rule set will redirect all page to http that are accessed by https except /page/contact.

Related

Https to http redirect using htaccess for a single PHP file

I am trying to redirect https://demo.aurazoscript.com/surf.php?id=15 to http://demo.aurazoscript.com/surf.php?id=15. I tried the following in .htaccess but that doesn't work. It turns off forced HTTPS redirection for the whole website which I don't want. I only want it to be turned off for that single page surf.php.
I tried in .htaccess
# FORCE HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} \/(surf.php)
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# DISABLE HTTPS
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !\/(surf.php) [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I am using Flexible SSL from CloudFlare.
I think you do it opposite way , try this at the beginning of .htaccess file after RewriteEngine On:
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{HTTPS} on
RewriteRule ^surf\.php http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTPS} off
RewriteRule !^surf\.php https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: clear browser cache then test .

Mod rewrite, redirect all to https except specific URI and redirect that URI from https to http

So I've been hitting a wall have have tried many different options, and configurations. I'm trying to redirect my site to https with the exception of one specific URI which I want to be redirected from https to http and then remain http. I would greatly appreciate any help. This is being done from the virtualhost configuration. Below are the settings I have tried in pairs with ssl.conf and mysite.conf. However I keep either getting a redirect loop or a page that doesn't load.
mysite.conf
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !/example
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^(/|page1|page2)
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !=/example
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{REQUEST_URI} =/example
RewriteRule ^(.*)$ http://my.site.com$1 [R,L]
Plus one of the above.
Then in ssl.conf
RewriteEngine on
RewriteCond %{REQUEST_URI} =/example
RewriteRule ^(.*)$ http://my.site.com$1 [R,L]
This should do it for you. Place the most specific rule (that for /example) first.
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^/?example$ http://my.site.com/example [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/?example$
RewriteRule ^/?(.*)$ https://my.site.com/$1 [R=301,L]
This should work in either server (.conf) or directory (.htaccess) context.

url rewriting to redirect www to non www on https

I just changed my website http to https. I'll already redirected http to https version. I also want to redirect www version to non www.
So far couldn't succeed it.
http://www.domain.com => https://domain.com
https://www.domain.com => https://domain.com
Here is the .htaccess file;
<ifModule mod_rewrite.c>
RewriteEngine On
redirect 301 /makale /sozluk
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
Thank you.
You should remove this bit:
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
And add this just under the RewriteEngine On:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.sporapp\.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
You need to have the redirecting happening before you rewrite, otherwise the request gets changed before it gets redirected.
Also, you have mod_alias directives which will interfere with the rewrite to /index.php:
redirect 301 /makale /sozluk
Should be:
RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]

unwanted loop in .htaccess

I want to use mod_rewrite to redirect all Urls in http to https except with a single directory.
In other words:
Any request for http content will redirected to the content in https
Any request for content in the blog directory (URI starting with /blog/) will be redirected to its http equivalent.
I have implemented the following .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Whenever I call a page with a request URI starting in /blog/ I get a redirect loop. I don't really understand why. What am I doing wrong?
Try this.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !blog [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirecting to https with wildcard urls (htaccess)

I have the following problem. I have a site where some pages must be redirected to the https-secured equivalent of the link.
I tried several things but in the end it seems that the server ends up in a loop redirecting :(
Now I redirect every page to the https page but that is not what I want to.
This is the code I use right now:
RewriteCond %{http_host} ^www.domain.nl
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.nl/$1 [L,R=301]
I would like to have al urls starting with bo- have https.
examples:
http://www.domain.nl/bo-users -> redirects to https://www.domain.nl/bo-users
http://www.domain.nl/bo-groups -> redirects to https://www.domain.nl/bo-groups
but
http://www.domain.nl/about-us -> stays at http://www.domain.nl/about-us
It is clear to me I need some wildcards in the rewrite condition for all urls startin with bo-.
We are using apache on the server.
Hope someone can send me in the right direction.
Thanks,
Frank
Update after the tips from Anubhava.
I current;y have this htaccess but I can't get it working (even when clearing the cache in my browser)
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
#rewrites een url to www.
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[a-z]{2,6}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Hope for some extra help!
Thanks,
Frank
I just opened another call. The initial answer worked fine!
Use this rule:
# force HTTPS for bo- URIs
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
# force HTTP for non bo- URIs
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]