Subdomain to folder in Plesk - apache

I use Plesk and I need to display the content of a folder in a subdomain.
I have the .htaccess file as follows:
RewriteEngine On
#Redirect HTTP and WWW to HTTPS without WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
#Redirect subdomain to folder
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^(.*)$ /sub/$1 [L,NC,QSA]
The first rules work correctly redirecting to the secure version with www. But the following rules don't work.
In the DNS records, I have created a CNAME record from sub.domain.com to domain.com, is this correct?
When accessing sub.domain.com from the browser, it redirects me to the Plesk login page (sub.domain.com/login_up.php), and any other files (existing in the folder) give a 404 error.
Thanks in advance for the help, I'm a web developer but I don't understand servers 😕

The solution is simple.
You have to create a subdomain of the main domain in Plesk and, in the hosting options, point to the folder as follows:
🏠/httpdocs/folder

Related

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

.htaccess settings for multiple websites with one document root

My Hoster allows multiple Website but just with one DocumentRoot for all domains.
What I want are site with different content:
https://www.site1.com
https://www.site2.com
What I have:
Directory Structure
/html/ <- Document root
/html/site1.com/ <- Symlink to /pages/site1.com/web
/html/site2.com/ <- Symlink to /pages/site2.com/web
How it should work:
Browser: https://www.site1.com/folder/
<- this will internally gets internally redirected to /pages/site1.com/web/folder/
The current .htaccess solution:
RewriteCond %{REQUEST_URI} !^/site1.com/.*$
RewriteCond %{HTTP_HOST} ^site1.com$ [NC]
RewriteRule ^(.*)$ /site1.com/$1 [L,NC]
It IS working, if I visit https://www.site1.com/folder/.
But the problem, what I dont want:
1.) If I visit site1.com/folder (without last slash), the webserver automatically redirects to https://www.site1.com/site1.com/folder/ instead of https://www.site1.com/folder/.
I think this is due to the auto trailing slash feature of directories from Apache (Shared Web Hoster with limited ssh account).
One possible solution would be to redirect 301 all web browser https://site/sitefolder.com/ request uri's to the non /site1.com/ request uri's.
But every htaccess rule i tested failed.
2.) I dont want to make the http://www.site1.com/site1.com & https://www.site1.com/site2.com urls accessable due to duplicated content.
How can I fix this problems and have one document root with multiple domains as subdirectories without duplicated content / redirects to the subfolders INSIDE the url?
This could not be done in 3 lines, so here is what I would do:
RewriteEngine On
# First we check if we have a direct access to our folder and prevent it with a 404 Error
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?site1\.com(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^/?site2\.com(/.*)?$ [NC]
RewriteRule ^.*$ - [R=404,L]
# Optional you can redirect all non-www to www, http or https stay the same
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ %{REQUEST_SCHEME}://www.%1/$1 [R=301,L]
# Now we rewriting all request based on the requesting host works with www and non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ /%1/$1 [L]
The answer assume that the folder is called the same as the host but without leading www., as you have it in your question. So for http://www.example.com the folder have to be example.com.

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

Redirection with .htaccess maintaining the subdomain url instead of the path

I made a subdomain site.domain.com.co and has its files at domain.com.co/site, and it redirects fine with this htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.domain\.com\.co$ [OR]
RewriteCond %{HTTP_HOST} ^www\.site\.domain\.com\.co$
RewriteRule ^/?$ "https\:\/\/domain\.com\.co\/site\/" [R=301,L]
But it still showing the path domain.com.co/site in the url, and i need it to show the subdomain i created site.domain.com.co.
A couple of weeks ago, the owner of this domain applied a security certificate (https) to the domain and he says the subdomain was displaying correctly (not the path) before the certificate was aplied (i'm not sure if it's true).
Thanks!
EDITED
I founded the answer. Here's the full code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.domain\.com\.co$ [OR]
RewriteCond %{HTTP_HOST} ^www\.site\.domain\.com\.co$
RewriteRule ^site\.domain\.com\.co?$ "https\:\/\/domain\.com\.co\/site\/" [R=301,L]

Subdomains Display The Wrong Content Using HTTPS

I am using a wildcard SSL on my main domain and a few sub-domains (apps, dashboard, support, training). The problem I am facing is that when I try to access a sub-domain via HTTPS, it shows the index/main page of the root domain, but when the sub-domain is accessed via HTTP, it shows the correct page. I would like to know what information I would need to put in the .htaccess file to make the sub-domains:
Automatically use HTTPS.
Show the correct content on that sub-domain when using HTTPS.
The sub-domains don't currently have an .htaccess file, because I have no idea what should be put in there at this point.
Here is an example of the HTTP sub-domain: http://goo.gl/Mcg66l
Here is an example of the HTTPS sub-domain: http://goo.gl/Aw9yl6
Here is the main domain: http://goo.gl/TfLjuy
The main domain is doing exactly what I want it to do (www to non-www and automatic https redirection), but the sub-domains are not doing what I want them to do, as described above.
Here is the .htaccess file on the main domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
Lastly, I am using cPanel under a reseller account serviced by HostGator.
Thanks for taking a look at this problem!
I had the same exact issue, also using a HostGator account. I found the answer on another question that you can find here.
They go about solving it by applying HTTPS for the main domain, forcing HTTP on a subdomain, and then as a bonus remove all WWW's
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]