.htaccess forcing HTTPS - .htaccess

I am trying to force HTTPS on a domain. It must be done using a method that works by domain name and not port number (due to host structure/setup).
My closest attempt was:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^.*$ https://www.mydomain.com/$1 [R=301,L]
This works when typing "mydomain.com" into the address bar, automatically redirecting to "https://mydomain.com" but when I type "www.mydomain.com" it does not work. I assume it is a syntax issue as I am very new to htaccess and have spent about 4 hours trying to create a solution from other's code.
Any chance of a pointer?
To make the setup a little more understandable.
/public_html/ - All files in this folder relate to www.mydomain.com
/public_html/subfolder - These folders contain files also relating to mydomain.com
/public_html/subdomain - These folders contain files relating to www.myotherdomain.com
My other domains are subdomains of mydomain.com for to be listed in the cpanel on the host. For example: subdomain.mydomain.com is the same as www.myotherdomain.com.
Hopefully that clears up the structure.

Your redirect happens whenever a request is made to the exact domain mydomain.com (that's what the RewriteCond is testing for). It doesn't apply to any other domains and doesn't detect HTTPS. Use this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

Related

How to redirect root domain to subfolder (with https) and rest of addon domains to subfolders (without https)

How to redirect root domain to subfolder (with HTTPS) and rest of other addon domains to subfolders (without HTTPS).
Currently I have this .htaccess in root which redirects with HTTPS to the-main-subfolder ok. But my other addon domain, say domain2 also gets redirected to the-main-subfolder.
I would like to redirect domain2 to the-domain2-subfolder without HTTPS.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch ^/$ /the-main-subfolder/
I am not sure if this code is correct as it might me using a wildcard. I got this code from searching on net but there are so many suggestions that I am confused now!
In summary: My main hosting account in root should go to https://www.domain1.co.uk/the-main-subfolder when user types in domain1.co.uk in browser and my addon domain http://domain2.co.uk should go to http://www.domain2.co.uk/the-domain2-subfolder.
You can use additional RewriteConds to define specific redirections:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^example1\.org$
RedirectRule ^(.*)$ /example1\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^example2\.org$
RedirectRule ^(.*)$ /example2\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host1\.example\.org$
RedirectRule ^(.*)$ /host1\.example\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host2\.example\.org$
RedirectRule ^(.*)$ /host2\.example\.org-subfolder/$1 [L,QSA]
I added a few examples to demonstrate the redability of explicit implementation and that you can do that for both, separate domains and hostnames (sometimes incorrectly called "subdomains"). I would always prefer such explicit notation over generic approaches since you can individually modify things, for example for testing or debugging purposes. Except if you are in a mass hosting situation obviously, then a database based approach makes sense.
Note that the redirection for what you call the "root domain" (example.org here) has a second RewriteCond now. Both conditions are AND-combined per default.
For safety you probably also want to add some more rules to redirect requests to something like https://example.org/host1.example.org-subfolder to the specific domain name, since according to your description you are limited to a single file tree in your hosting account. Same for request to http://test1.example.org/test1.example.org-subfolder/... to eliminate the literal folder name.
Oh, and a warning: the above syntax works for .htaccess style files only. If you have access to the real host configuration then you should always prefer to place such rules in there. However you need a slightly changed syntax then. .htaccess style rules are notoriously error prone, hard to debug and they really slow down the http server. They are only offered as a last option for those without access to the host configuration.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain1.co.uk$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
Thanks #arkascha - Everything now works as expected with the above code. I suppose we do not need to mention so called add-on domains here at all because cPanel handles the sub-directories for them internally when we add subsequent domains on the hosting package (i.e. addon domains)!
Just to update that my previous solution partially works as it has few niggles/bugs. So went back to the drawing board and suddenly realised I was unnecessarily trying too hard!!
Deleted the old htaccess file first and followed instruction below..
The solution is already provided by cPanel in something called "Redirects" in Panel Icons.
I just had to enter everything in user interface text boxes like choose domainname = "domain1", old folder = "\", new folder = "https://www.domain1.co.uk/the-main-subfolder" - And just click create the redirect. In doing so it creates a .htaccess file itself automatically. I am sharing this below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.co\.uk$
RewriteRule ^/?$ "https\:\/\/www\.domain1\.co\.uk\/the-main-subfolder\/" [R=301,L]

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

.htaccess redirect to a subdirectory for a specific web address

I require some guidance with redirects please:
I have two WordPress installations on one hosting - one each in a separate sub-directory.
I was able to get the control panel to set up redirects for each domain, so that - for example:
www.domain1.com redirected to www.domain1.com/d-one/
and
www.domain2.com redirected to www.domain2.com/d-two/
Which works fine - I'm not too bothered that the path contains a subdirectory (I assume I can't hide it).
However, for convenience of the visitors, I would like to set up redirects so that for example:
www.domain1.com/contact redirects to www.domain1.com/d-one/contact
and
www.domain2.com/contact redirects to www.domain2.com/d-two/contact
... the redirect knows which domain is which.
Seeing as no-one was in any hurry to help :( , I did my own bit of digging elsewhere (something I probably should have done earlier if I weren't in such a flap)...
After exhausting all the threads on this site, I found this site:
http://corz.org/serv/tricks/htaccess2.php
which sorted me right up with this solution under "multiple domains in one root":
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]
RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]
and it also helped me to hide the unwanted subdirectories d-one and d-two.
Loverly jubbly!

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Pointing a subdomain to a subfolder using .htaccess

My webhost automatically forwards all requests to *.mydomain.com to the toplevel domain mydomain.com.
I wanted to map any subdomain to a specific folder on my toplevel domain. i.e. sub.example.com must be mapped to example.com/someFolder (without change in the address bar).
After digging around on the net, I came up with this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.example\.com.*$
RewriteRule (.*) http://example.com/myfolder/$1 [L]
This seems to work well, except for one problem: When I go to the URL sub.example.com, the URL in the address bar changes to example.com/myfolder . But, when I do something like sub.example.com/login - this maps to "example.com/sub/login" properly without the change in the address bar. Any help greatly appreciated!
only small change needed:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.example\.com.*$
RewriteRule (.*) myfolder/$1 [L]
stripped out http:// at the rule which tells Apache to send a Redirect header instead of proper server-side rewrite.

Resources