Redirect https .htaccess - .htaccess

Heey everyone so i recently added https to a website and now i have a small problem with the redirect if someone goes to the website without www.
they go to the following url
example: https://www.example.com/https://example.com
instead of going to https://example.com
Code:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^koenschenk.nl [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]
{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://www.koenschenk.nl/$1 [L,R=301,NC]
Redirect 301 https://koenschenk.nl https://www.koenschenk.nl
ErrorDocument 404 /404.php

You can setup redirection from HTTP to HTTPS by editing the following code in your /etc/apache2/apache2.conf (supposing you're using a Linux machine.):
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule / https://{koenschenk.nl}/
Place the mentioned lines of code in the .htaccess file in your website’s root folder.

Related

Stop https for a referer

I have a https website which has a hidden user area. From there the users has a link which links to a http site. With a referrer the site makes sure that the userers are coming from my site. Other access attemps are blocked.
Since my webstie change to https, there referrer is not working anymore.
My solution attempt so far:
Inside my htaccess I want to stop the https for only one file on the site. Where the link is. My htaccess so far.
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/Mitgliederservice/Infoline/index\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
I don't understand whether you want to exclude one page or force only one page to https so , if you want to exclude one specific page use the following code :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/path/to/yourpage\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
otherwise use the following code :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/path/to/yourpage\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

Force redirect to https by htaccess

I have domain and a subdomain like example.com and blog.example.com and enabled ssl for both using wildcard on bluehost. I add following code suggested by bluehost
Custom subdomain .htaccess SSL + WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
# End custom subdomain .htaccess
# Custom maindomain .htaccess WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
sites are running on https properly but they are accessible on http as well.
i tried following code to force redirect but its not working.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
also i have two htaccess file for main domain and for subdomain and above code is placed in main domain htaccess file.
am i doing something wrong or missing something to force all urls to open with https:// ?
Have you tried this :
For only domain
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
For subdomain(place .htaccess inside subdomain folder)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blog.example.com/$1 [R,L]

Remove SSL integration from a specific folder using htaccess

I have an integrated SSL for my entire site and have placed htaccess code to redirect to https when anyone visits my domain URL. But I want to keep one folder out of this redirection to https. Please help me with this... Below is the htaccess code placed in my root to redirect all requests to https counterpart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Thanks
Just add a condition to exclude the folder:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And if you wanted to redirect SSL requests to non-SSL for /folder1, then:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/folder1
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I want to redirect all http requests to https, and all www requests to non-www

Here is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>
I want to redirect all http requests to https, and all www requests to non-www, and all file requests to index.php
For example:
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Everything seems to be working except the www part.. Any help please?
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Maybe this will work:
RewriteEngine On
# Remove www from https requests.
# Next 3 lines must be placed in one .htaccess file at SSL root folder,
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
# Redirect all http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (?:www\.)?(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
If it doesn't work, try replacing
RewriteCond %{HTTPS} off or on with
RewriteCond %{HTTP:X-Forwarded-SSL} off or on
You can add an additional rule dealing with the www part
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
The RewriteCond captures everything after the www. and uses that in the RewriteRule as %1.
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

Redirect rule for only home page from http to https

Can we make https for only home page i.e., for
example.com or http://example.com or www.example.com or http://www.example.com
remaining all pages should start with http.
I have tried like this but it isn't worked
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} "^/$"
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI}
Not really tested but should do the magic
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?$ https://%{HTTP_HOST}/ [R,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule !^/?$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
You can also redirect like this in a .htaccess at root level:
DirectoryIndex index.html
Redirect 301 /index.html https://www.website.com/
This is not secure. Read OWASP a9 and set the HSTS flag to make your entire website HTTPS only.
HTTP is a mistake and if you are doing this for "performance" you are a fool. HTTPS is extremely light weight.

Resources