How can I make a specific page not https - .htaccess

So, I use a chat script called AJAX-Chat
it works fine, but only when I take away the part of my .htaccess file that makes my site always go to https://
I need a way for a SPECIFIC directory to not be https://
My .htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Add this above your other rules:
RewriteRule ^(Directory_to_exclude)($|/) - [L]
it will exclude that directory from forced to HTTPS.
Alternate(ther are lots)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !exclude_me [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Multiple redirects in .htacces (http to https; .html to .php; index to /)

For re-directing all requests without “www.” or with “http:” I was using the following .htaccess settings since activating SSL:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php/(.+)$ /$1 [R=301,L]
RewriteRule ^index\.php/?$ / [R=301,L]
I now noticed that my former redirections of index.php to / don’t work anymore, if the requested URL is http.
Also, I now need to change current .html files (half of my website) to .php and add this:
RewriteRule ^(.*)\.html$ $1.php [L]
I would like to use as little rewrites as possible to maximize SEO effects, but I got stuck trying around. Any help would be greatly appreciated.
After trying around for some more hours, I think I came to an elegant solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)index\.php/?$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)index\.html/?$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]
# index.php or index.html are sent to https://www.domain.tld/
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^(.*)\.html$ https://www.%{HTTP_HOST}/$1.php [R=301,L]
# .html is changed to .php and additionally redirected to https://www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Everthing not redirected before, gets an additional www. and/or is redirected to https.
I'm happy about comments and recommendations to improve further, if neccessary. Hope this is helpful to anyone.

.HTAccess multiple redirect

I want to achieve that if a user write:
http://www.example.com/url/index.html
http://www.example.com/url/
https://example.com/url/index.html
https://example.com/url/
will be always redirect to:
https://www.example.com/url/
So I write this redirect istruction in .htaccess (I'm not an expert of this so I think it could be here the problem) and it seems to work:
RewriteEngine On
RewriteCond %{ENV:HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
The problem is that from some SEO tools it seems that it make the work in more than 1 redirection. Infact I think that if the user look for:
http://example.com/url/
it redirects to
https://example.com/url/
after to
https://www.example.com/url/
How can I change the previuos code to make it better and make everything with 1 redirection?
Thanks
You can shorten your http to https redirect rules. Instead of using two separate rules for http and htttps://www you can just use a single rewrite rule that will handle both in one redirection .
RewriteEngine on
# http to https and non-www to www
RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [NE,R=301,L]
# remove index.html
RewriteRule ^(.*)\.index\.html$ /$1 [L,R=301]
Clear your browser cache before testing this htaccess.

.htaccess rewriting www to http:// only and allowing HTTPS too

I have this rule in my .htaccess
It worked fine while I used only HTTP version.
But now I need HTTPS too.
Right now i'm always redirected to http version of my web.
I need:
If its https i need only execute last rule (last line)
If its http i need execute these three lines
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteRule ^(.*)$ /www/$1 [L,NE]
Can you please suggest changes into my .htaccess file?
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^(.*)$ http%1://domain.com/$1 [L,R=301]
RewriteRule ^((?!www/).*)$ /www/$1 [L,NC]

Force https except for one directory

I'm using the following .htaccess file to force any page request to https://
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How could I force https for all directories/pages except the directory "/domain" ?
I can change the main htacces or create a new one in the /domain directory, the easy way, but how?
This should get what you need.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !domain [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess redirect subfolder to HTTPS

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.
For example, whenever someone accesses: http://www.mydomain.com/subdirectory (and any other sub-directories of that "subdirectory").
I'd like it to rewrite to https://www.mydomain.com/subdirectory
I've tried the following, but it appears to create a loop:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Also, this .htaccess file is placed in the root of my domain.
Any ideas on how to modify my RewriteRule?
Many Thanks!
I would put this into the domain's root directory:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!
You'd want to add the subdirectory back in as per Floern's response, see below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Add the lines given below to .htaccess file of that subdirectory:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
Try This
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
This will remove www prefix , and force https:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]
Change example.com for you main domain and subdirectory for Sub Directory
Try
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

Resources