Creating a subdomain through vhost or htaccess - apache

Currently working on a project on my local xampp setup. I am using a test domain like so: testsite.devs. Now, I want to create an admin interface using the subdomain admin.testsite.devs. I have it working, but Im currently using vhost like so:
<VirtualHost testsite.devs:443>
DocumentRoot "D:/websites/testsite"
ServerName testsite.devs
ServerAlias testsite.devs
ErrorLog "logs/testsite.devs-error.log"
CustomLog "logs/testsite.devs-access.log" combined
<Directory "D:/websites/testsite">
Require all granted
</Directory>
SetEnv ENVTYPE "dev"
SSLEngine on
SSLCertificateFile "crt/testsite.devs/server.crt"
SSLCertificateKeyFile "crt/testsite.devs/server.key"
</VirtualHost>
<VirtualHost admin.testsite.devs:443>
DocumentRoot "D:/websites/testsite/admin"
ServerName admin.testsite.devs
ServerAlias admin.testsite.devs
ErrorLog "logs/admin.testsite.devs-error.log"
CustomLog "logs/admin.testsite.devs-access.log" combined
<Directory "D:/websites/testsite/admin">
Require all granted
</Directory>
SetEnv ENVTYPE "dev"
SSLEngine on
SSLCertificateFile "crt/testsite.devs/server.crt"
SSLCertificateKeyFile "crt/testsite.devs/server.key"
</VirtualHost>
It appears to work, but Im thinking if I dont have access to a vhost file, is there a easier way to just do this via the htaccess file? I tried examples found online like so:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^admin.testsite.devs
RewriteRule ^(.*)$ testsite.devs/admin/$1 [L,NC,QSA]
But if I go to:
admin.testsite.devs/index.html
The URL changes to:
testsite.devs/admin/index.html
So using htaccess, how do I create the sub domain where the URL stays the same? So if I go to admin.testsite.devs/index.html, it stays that way in the URL bar?

Related

Keep original URL after redirection by domain provider

Goal
fharrell.com/* is redirected by the domain provider to hbiostat.org/blog/*. I want to keep the address bar showing fharrell.com/*
Apache2 Setup
/etc/apache2/apache2.conf is standard with the following exception:
<Directory /home/ubuntu/htdocs/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
/etc/apache2/sites-enabled/hbiostat.org.conf is symbolically linked from /etc/apache2/sites-available/hbiostat.org.conf
hbiostat.org.conf sets the document root as /home/ubuntu/htdocs which has been working well for some time
Contents of hbiostat.org.conf:
<VirtualHost *:443>
ServerAdmin my#email.address
DocumentRoot /home/ubuntu/htdocs
ServerName hbiostat.org
ServerAlias www.hbiostat.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/htdocs>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hbiostat.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hbiostat.org/privkey.pem
</VirtualHost>
<VirtualHost *:80>
ServerName hbiostat.org
ServerAlias www.hbiostat.org
DocumentRoot /home/ubuntu/htdocs
<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
</VirtualHost>
Systax was checked using sudo apachectl -t.
I checked that mod rewrite is active using sudo a2enmod rewrite and restarted the server with sudo systemctl restart apache2
But this has no effect, with hbiostat.org/blog/* remaining in the addressbar.
Tried:
Many remedies on stackoverflow.com (including the two below) and elsewhere, including putting the commands into an .htaccess file (I'd like to avoid the .htaccess approach).
Any help appreciated.
Redirect domain but keep original url
Redirect subfolder URL but keep original domain name
You can't make the browser display a different domain after a 30x redirect.
mod_rewrite doesn't do what you're thinking it does.

Alias inside VirtualHost is not working and gives 404 error

I have a server which maintains multiple domains using VirtualHost and I'm trying to setup GitWeb according to Gitweb - ArchWiki.
According to the wiki, I have to append the lines below to an Apache config file.
Alias /gitweb "/usr/share/gitweb"
<Directory "/usr/share/gitweb">
DirectoryIndex gitweb.cgi
Options ExecCGI
Require all granted
<Files gitweb.cgi>
SetHandler cgi-script
</Files>
SetEnv GITWEB_CONFIG /etc/gitweb.conf
</Directory>
After this, Alias works and I can access the service by accessing one of
192.168.10.102/gitweb/ (local IP)
https://my_domain_1.com/gitweb/
https://my_domain_2.com/gitweb/
...
So far, everything's okay.
Now I'd like to limit the effect of the Alias to one domain: my_domain_1.com. So I moved the lines above inside its existing VirtualHost settings.
<VirtualHost *:80>
DocumentRoot "/var/www/html1"
ServerName my_domain_1.com
# The lines are pasted here.
RewriteEngine on
RewriteCond %{SERVER_NAME} =my_domain_1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} END,NE,R=permanent]
</VirtualHost>
However, when I access https://my_domain_1.com/gitweb/, 404 error occurs. The error log says nothing and I have no .htaccess for the domain.
How can I make it work?
I found the solution.
Since I requested that all of http requests should be redirected to the https versions, I should have written the gitweb settings not in <VirtualHost *:80> (http) but in <VirtualHost *:443> (https). The result should be
<VirtualHost *:443>
DocumentRoot "/var/www/html1"
ServerName my_domain_1.com
# The lines are pasted here.
SSLCertificateFile /etc/letsencrypt/live/my_domain_1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/my_domain_1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
Now it works perfectly.

Problem with redirecting to main page with apache and nestjs

I'm looking for solution for my issue, because I've tried everything that comes to my head and I'm still not able to solve it.
So, I have backend written with nestjs and on my local machine everything works as expected.
But when I run it on server and type just host name ex. https://example.com it shows empty page.
It is confusing because every other route works well except main one.
So I'm assuming there's something wrong on my apache virtual host configuration but I'm quite new to it and I don't know where the problem is.
My apache2 conf looks like this:
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/example.com.key
SSLCertificateChainFile /etc/ssl/example.com.ca-bundle.crt
ServerAdmin admin#example.com
ServerName example.com
DocumentRoot /var/www/example.com/public_html/
ProxyRequests Off
ProxyPreserveHost On
ProxyVia full
<Proxy *>
Require all granted
</Proxy>
<Location "/">
ProxyPass http://127.0.0.1:50000/
ProxyPassReverse http://127.0.0.1:50000/
</Location>
<Directory /var/www/example.com/public_html/>
Allowoverride all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<IfModule mod_ssl.c>
My nestjs app starts on port 50000.
Thanks for any ideas.
Finally I figured it out.
Error was caused by useStaticAssets() function. When I removed it, everything starts working properly.
To be able to still serve static files I changed it to nest's ServeStaticModule.
Cheers.

Unable to get Apache docker container to serve virtual host with subdomain

I have a site I currently run that works well, but both to learn and to make it more portable, I've been trying to dockerize it. I'm using the offical apache and php images, and setup my virtual hosts identical to how I have on the running site, just with different domains. Unfortunately, while I can get one to work, the second does not.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName api.gamersplane.local
DocumentRoot /var/www/api
ErrorLog "/var/log/gamersplane.api"
CustomLog "/var/log/gamersplane.api" common
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/api/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
<VirtualHost *:80>
ErrorLog "/var/log/gamersplane"
CustomLog "/var/log/gamersplane" common
ProxyPreserveHost On
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ServerName gamersplane.local
ServerAlias *.gamersplane.local
DocumentRoot /var/www
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
Originally, I had the first vhost (api.gamersplane) second, but thought maybe it was the server alias that was the problem. Then I switched positions, and even commented out the base (gamersplane.local), but had no luck. This works on the running site, so I can't figure out what's wrong with it.
Checking the headers returned by Postman, I do see it has the Apache and PHP headers, so it seems to be hitting SOMETHING, I just don't know what.

Apache: SSL certificate not working with www, how to remove?

Im trying to setup our VPS with a newly installed SSL certificate. The certificate is for only domain.com, not www.domain.com. So when a user visits https://www.domain.com they now get an "insecure page" warning - which I of course don't want to have. I want to redirect the user to https://domain.com, but I keep getting stuck in infinite loops all the time..
Here's my virtual host file..
<VirtualHost *:80>
ServerAdmin my#email.com
ServerName domain.com
ServerAlias domain.com
Redirect permanent / https://domain.com/
DocumentRoot /var/www/domain.com/
<Directory /var/www/domain.com/>
AllowOverride ALL
</Directory>
ErrorLog /var/www/logs/domain.com/error.log
CustomLog /var/www/logs/domain.com/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin my#email.com
ServerName domain.com
ServerAlias domain.com
DocumentRoot /var/www/domain.com/
<Directory /var/www/domain.com/>
AllowOverride ALL
</Directory>
ErrorLog /var/www/logs/domain.com/error.log
CustomLog /var/www/logs/domain.com/access.log combined
SSLEngine on
SSLProtocol all
SSLCertificateFile /etc/ssl/mycert.crt
SSLCertificateKeyFile /etc/ssl/mykey.key
SSLCACertificateFile /etc/ssl/myca.crt
</VirtualHost>
Im a beginner at this, and I can't seem to find a solution besides buying a wildcard certificate which I really cannot afford..
Thank you very much in advance!
You might needed to redirect all your www url calls to non-www.. Try the following rewrite rules with your .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/ [last,redirect=301]
Replace example.com with your domain name and make sure to put this in the .htaccess at the document root of your website.