.htaccess SSL HTTPS For One Domain Hosted on Shared Server - .htaccess

I have searched like crazy to find an answer for this... so here goes the scenario:
I have a server that hosts several domains. However, the main domain should be the only one loading as HTTPS.
Whenever I try the following code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.MAINDOMAIN.com/$1 [R,L]
The rest of the domains change from http://www.site.com to HTTP(S)://www.MAINDOMAIN.com/FolderName and so on.
How can I go about this?

The most straightforward way to do this is to have only one virtual host, www.maindomain.com, available on port 443, and have only www.maindomain.com:80 redirect to https.
<VirtualHost *:80>
ServerName www.maindomain.com
Redirect / https://www.maindomain.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.maindomain.com
SSLEngine on
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.site.com
...
</VirtualHost>
No RewriteRule is needed. If you want you can also have HTTPS on the other sites redirect to HTTP:
<VirtualHost *:443>
ServerName www.site.com
SSLEngine on
Redirect / http://www.site.com/
</Virtualhost>
Otherwise https://www.site.com will be served by https://www.maindomain.com, since that will be the only virtual host available by HTTPS. That will make it the default virtual host for HTTPS, so it will serve all HTTPS requests.

Related

Redirect all requests from port 80 to 443

I have a vps setup with proxy configured to redirect to my home server via vpn.
How do I disable port 80 so when the user types in browser http://my.domain he will be redirected to https://my.domain ? Or how do I enable secure connection only ?
As an example look at http://vk.com - even when you open http link it creates secure https connection.
If I just keep <VirtualHost *:443> and remove <VirtualHost *:80> from /etc/apache2/sites-available/myconf.conf it shows default apache2 page when I open http://my.domain
I found solution.
<VirtualHost *:80>
ServerName my.domain
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

Proxyreverse in Apache2 with wordpress

I have installed and configured Wordpress on my server using also apach2 virtualhosts.
I made a virtualhost with this config
<VirtualHost *:80 *:443>
ServerAdmin yourluxuryroad#gmail.com
ServerName yourluxuryroad.com
ServerAlias www.yourluxuryroad.com
DocumentRoot /var/www/yourluxuryroad
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.yourluxuryroad.com [OR]
RewriteCond %{SERVER_NAME} =yourluxuryroad.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /node-yrl-book http://localhost:5000
ProxyPassReverse /node-yrl-book http://localhost:5000
</VirtualHost>
<Directory /var/www/yourluxuryroad/>
AllowOverride All
</Directory>
As you can see from the config i'm trying to set the ProxyPass directive for redirect the requests recived on the path /node-yrl-book to a nodejs service ( made using expressjs ) at port 5000 but this is not working, instead of getting a redirect to that service i get the 404 Page not found wordpress page.
If I make a request at my_ip/node-yrl-book instead it works correctly and i am redirected to the service at port :5000
I suppose that i'm missing something in my configuration but i'm not understanding what..
Maybe is something in wordpress that has to be changed?
You have way too much going on.
ProxyPass -or- DocumentRoot, not both.
You can either serve the page from apache (by using DocumentRoot), or you can serve the page from nodejs (by using ProxyPass).
Finally i solved this, I made an SSL certificate for my website using let's encrypt certbot, This script created a new virtualhost in another file for the https requests ( called /etc/apache2/sites-available/myDomain-le-ssl.conf ) That virtualhost was overriding my proxypass directive, editing also this virtualhost made all work

HTTP of domain-1 loads domain-2 under HTTPS

I have set up an ubuntu 14 as a server and pointed domain-1 without SSL but when I try accessing domain-1 with https:// in the URL it takes me another website(domain-2) hosted on the same server which is configured with SSL rather than showing Page not found error.
How do I avoid this? Here is a list of things that I have tried.
.htaccess won't work to redirect HTTPS to HTTP as the port 443 is not configured for domain-1 under V-Host file to reach the .htacess file.
Cannot setup <VirtualHost *:443> without an SSL and write a redirect here.
Have I done anything wrong in the code below?
domain-1.conf
<VirtualHost *:80>
ServerAdmin admin#domain-1
ServerName domain-1
ServerAlias www.domain-1
DocumentRoot /var/www/domain-1/
ErrorLog /var/www/domain-1/error.
CustomLog /var/www/domain-1/access.log combined
</VirtualHost>
domain-2.conf
<VirtualHost *:80>
ServerAdmin admin#domain-2
ServerName domain-2
ServerAlias www.domain-2
DocumentRoot /var/www/domain-2/html
ErrorLog /var/www/domain-2/error.
CustomLog /var/www/domain-2/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin admin#domain-2
ServerName domain-2
ServerAlias www.domain-2
DocumentRoot /var/www/domain-2/html
SSLEngine on
SSLCertificateFile "/var/www/domain-2/ssl/domain-2.crt"
SSLCertificateKeyFile "/var/www/domain-2/ssl/domain-2.key"
SSLCACertificateFile "/var/www/domain-2/ssl/domain-2.ca-
ErrorLog /var/www/domain-2/error_ssl.log
CustomLog /var/www/domain-2/access_ssl.log combined
</VirtualHost>
I think this is one way to resolve this error, by the help of .htaccess file (HTTPS to HTTP redirect) but we will need to install a self-signed certificate.
Reason? As we access domain1.com the files that are displayed are from domain2.com so even if we create a .htaccess redirects on doamin1.com it won't take effect in order to avoid this, a self-signed certificate will help us in accessing the file from domain1.com and then the redirects on .htaccess file will take effect.
HTTPS to HTTP redirect
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This video will help you in creating a self-signed certificate.
https://www.youtube.com/watch?v=JLTWa62D0y0

Apache and Node on same IP

I have a single VPS with one IP. I'm using Apache to serve cloud.mysite.com and I have a NodeJS application listening on port 3000.
I'm trying to configure my VPS in a way so that when I visit mysite.com, I get my NodeJS application.
Instead, when I visit mysite.com, I'm forwarded to cloud.mysite.com.
My .conf files are below.
mysite.com.conf
<VirtualHost mysite.com:80>
ServerName mysite.com
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
cloud.mysite.com
<VirtualHost cloud.mysite.com:80>
ServerName cloud.mysite.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =cloud.mysite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
<VirtualHost cloud.mysite.com:443>
# Basics
ServerName www.cloud.mysite.com
ServerAlias www.cloud.mysite.com
# Next line puts ownCloud at the domain root instead of a /owncloud/ subdirectory (e.g. example.com vs. example.com/owncloud/)
Alias /owncloud "/var/www/owncloud/"
DocumentRoot /var/www/owncloud
# SSTL STUFF GOES HERE
# ownCloud
<Directory /var/www/owncloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/owncloud
SetEnv HTTP_HOME /var/www/owncloud
</Directory>
</VirtualHost>
SSLStaplingCache shmcb:/var/run/ocsp(128000)
Edit: I resolved this by clearing my cache.
It seems that it should proxy requests to your Node app if it's running on and listening on port 3000 on the same host, or fail if it isn't - but not to proxy requests to the other virtual host.
Make sure that you restarted Apache or made it reload the configuration after you made those changes and that your new config file is where it should be and is enabled.

godaddy subfolder domain htaccess redirect without changing browser url

Godaddy Deluxe Hosting with Linux
Main domain: www.domain1.com & 2 other hosted domains (www.domain2.com and www.domain3.com) are in subfolders
The contents of www.domain2.com .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ https://www.domain2.com/shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This will direct www.domain2.com/shop/tackle/1/0/
TO
www.domain2.com/shop/products.php?subcat_id=1&subcat2_id=0
but changes the url. I do understand that is because I'm includeing the https://www.domain2.com in the rewrite. However I try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
I get a 404 error. I does indicate that URL /domain2/shop/tackle/1/0/ can't be located.
Obviously the rewrite is including the domain2 folder name. I'm trying to keep the requested URL www.domain2.com/shop/tackle/1/0/ in the URL.
What I am missing?
If you have each domain pointing to correct DocumentRoot. as example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain1.com
DocumentRoot "/var/www"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
DocumentRoot "/var/www/domain2"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain3.com
DocumentRoot "/var/www/domain3"
</VirtualHost>
Then I don't think how the /domain2/ would be added onto relative request. But, if it is, then you should try RewriteBase directive to help with relative substitution is built correct. Like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This link has more information:
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritebase

Resources