.htaccess redirect subfolder to HTTPS - .htaccess

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]

Related

Forcing SSL and WWW using .htaccess for a subdomain

I have hosted opencart website on subdomain beta.mydomain.com with ssl cirtificate installed.
I want to force https and www . when someone types beta.example.com should take them to https://www.beta.example.com
I tired following .htaccess entries
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example.com\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteBase /
I dont understand whats going wrong here. thanks in advance.
Well, the domain you're using in your example is not beta.example.com it's example.com you should change it to the domain you require. Something like:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.beta\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.beta.example.com/$1 [L,R=301]
RewriteBase /
What do you use the RewriteBase for?
I personally like to use the following process:
Redirect to www
Redirect to https
Something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
In case you haven't already checked it, this GitHub Repository has some useful info on .htaccess.
The easiest way is to update your config.php file in your home directory.
I don't have it in front of me, but you'd want to change your HTTP and HTTPS url to "https://www.beta.example.com".
for htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://www.yoursite.com%{REQUEST_URI} [NS,R,L]
alternative htaccess code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
for php redirect
Open index.php file
find
<?php
add after
if (substr($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {
header('Location: http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 's':'').'://' . substr($_SERVER['HTTP_HOST'], 4).$_SERVER['REQUEST_URI']);
exit;
}

.htaccess force ssl execlude some url(s)

I'm trying to redirect every request to "https" instead of any call to "example.org/forum/...", but hunderets of solutions did not work and I can't figure out why for example this approach does not work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.org$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/forum/.*$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
EDIT: I've tried this example now on a domain with just some folders, index.html files and a .htacess file with nothing but these two rules in the root and works like charm!
The point is that I try to achieve this on a domain with a cms underneath, it is a contao installation and there it does not work. I also tried it on a wordpress installation and there it does not work too!
So I think it has something to do with internal redirections, does anyone has a clue!? I can't find such a case on the web...
The RewriteCond directive REQUEST_URI is starting with /, you should make these rules change:
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.org/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/forum/.*$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomaim.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteRule ^$ folder name [L]
It works for me redirect all to https
Foldername is the folder after youdomain.com/foldername

Redirecting to https and subfolder .htaccess

Situation: afwezigheden.eu is the main domain with a few coupled domains (= aliases?) like skatespots.be, inviteme.eu, halftwaalf.be... Now I'm trying to redirect those aliasses to a subfolder, without the users seeing it.
Al those sites are in a subfolder _DOMAINNAME. For example: skatespots.be is in afwezigheden.eu/_skatespots
Also I have an SSL certificate, so I would like to redirect all domains to https too.
I tried several things, the last one is this one, where skatespots seems to work and everything is redirected over https.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/_skatespots/
RewriteCond %{HTTP_HOST} ^(www\.)?skatespots\.
RewriteRule ^(.*)$ /_skatespots/$1 [L]
RewriteCond %{REQUEST_URI} !^/_halftwaalf/
RewriteCond %{HTTP_HOST} ^(www\.)?halftwaalf\.
RewriteRule ^(.*)$ /_halftwaalf/$1 [L]
RewriteCond %{REQUEST_URI} !^/_inviteme/
RewriteCond %{HTTP_HOST} ^(www\.)?inviteme\.
RewriteRule ^(.*)$ /_inviteme/$1 [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am i doing wrong?
What if you put the folder structure as this:
afwezigheden.eu/sites/skatespots.eu
afwezigheden.eu/sites/halftwaalf.eu
And then use this in your afwezigheden.eu/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !sites/
RewriteRule (.*) /sites/%{HTTP_HOST}/$1 [L]

How can I make a specific page not https

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]

.htaccess RewriteRule subsubdomain.subdomain.domain.tld to subdomain.domain.tld/subsubdomain

Is it posible to redirect a sub-subdomain into a subdomain with the sub-sub domain as new folder using .htaccess rewrite-rule?
For example... When i go to 2013.archive.example.com I want to end up in archive.example.com/2013..
Already tried some things, current .htaccess is:
RewriteRule On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$
RewriteRule ^(.*)$ archive.example.com/%1/$1 [L]
Unfortunately it isn't working. Any suggestions for this?
Changed it a but, currently using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
and is working exactly how I wanted:)
For external redirect: (URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
This will work provided DOCUMENT_ROOT is same for anything.archive.example.com and archive.example.com.
For internal forward: (No URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.archive\.example\.com$ [NC]
RewriteRule ^ /%1%{REQUEST_URI} [L]
If they share the same root, then you don't need the archive.example.com part in your rule's target:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Resources