.htacess redirect for ip to https doesnt work well - apache

I have tried to move my wordpress website to https.
I have dedicated ip also first i had this code below. But did'nt resolve my problem exactly
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Later on I had this code which resolved url redirection problems But later on ı had the same problems about when somebody cliks a http link given before doesnt go to https url.
<IfModule mod_rewrite.c>
RewriteEngine on
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
And another question is how i will redirect ip to https? And where i must put that?

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
should indeed work

I used a plugin called Really simple SSl
And that entered this code.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
and before all (as mentioned by Michael Berkowski )
RewriteCond %{HTTP_HOST} 11\.11\.11\.11
RewriteRule ^(.*) https://www.example.com/$1 [L]
thanks to participants

Related

www and non-www http requests to https://www with full path

I created redirect from non-www and www http request to https and www request that was quite easy. I used following rules in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
But now the problem is when someone enters old non-secure url like domain and project-about.html at the end .htaccess redirect to https but adds request at the end again so the request after the domain look like:
/project-about.htmlproject-about.html
How can I fix it? Thanks
Change it to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
This method removes the need for an extra RewriteRule also, helping to speed up your website.
Make sure you clear your cache before testing this.

htaccess RewriteRule to https doesn't work combined with other rules

I'm trying to set a few rules in my .htaccess to work in a specific way:
Specific page redirections:
RewriteRule "^web/site/mais$" https://www.main.com/front/for-you/insurance/ [R=301,L]
Home redirection
RewriteRule ^$ https://www.forexample.com.br/for-you/ [R=301,L]
RewriteRule ^/$ https://www.forexample.com.br/for-you/ [R=301,L]
HTTP to HTTPS and add www to the rest (this is what doesn't work), i tried many things:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Other way I tried forgotting the www rule (but doesn't work too):
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The only way I found to combine is writting the general https and www rule in the httpd.conf and the other rules into the .htaccess, but this way I get two or more 301 redirects, bad business for SEO.
Do you have the same problema with all the directories or only with some?
Because if you have ProxyPass (as you said), maybe it send you the redirections directly to Tomcat, so maybe you need to specify the rules into the ssl.conf to read that rules before leave the ssl.conf. Try to write the last you wrote into the ssl.conf after the ProxyPass
If you want to redirect some spacific pages to https, forexample, To redirect :
http://example.com/page.html
to
https://www.example.com/page.html
And
http://example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]

Forcing https on the whole site but not on one folder

Could anyone tell me how I can force https on my whole website but not on a single folder or url.
At the moment I have this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]
But if I add this second code in the root htaccess to remove https from the /thatsite.com/printing folder, I get a redirect loop because I am forcing on the code http to https and not https to http...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Do you know a way around this please? I have been looking all over the internet and cannot find a single good answer.
Try these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I solved this issue by placing this code in an .htaccess file the root folder of the directory that hosts the http site (e.g. public_html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]

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.

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]