I have setup on a xampp install a wildcard vhost on xampp in windows because I wanted to test some apps in localhost without having to add a too many lines to vhosts.conf for each and every app. I figured this would be the easiest and simplest way to do this was to add the following to my vhost.conf:
<VirtualHost *:80>
ServerAlias *.localhost
VirtualDocumentRoot "C:/xampp/htdocs/%1/"
</VirtualHost>
<VirtualHost *:443>
ServerAlias *.localhost
VirtualDocumentRoot "C:/xampp/htdocs/%1/"
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
<Directory "C:/xampp/htdocs/%1/">
AllowOverride All
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This let's me access superawesomeapp1 at the foldername in htdocs as a subdomain of localhost. So anything in foo is at foo.localhost and bar to bar.localhost and so on.
This worked well until I had an app that wanted the following added to vhost.conf:
<VirtualHost *:80>
ServerName johnny.localhost
ServerAlias johnny.localhost
DocumentRoot "C:/xampp/htdocs/heyjohnny/web"
<Directory "C:/xampp/htdocs/heyjohnny/web">
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory "C:/xampp/htdocs/heyjohnny/web/bundles">
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
</VirtualHost>`enter code here`
The vhost server alias overrides the latter, but I cannot figure out how to override the former short of commenting it out and manually mashing at the keys for every vhost I want to add which adds up after a while. I noticed it also overrode any attempt to redirect http to https. I might be running some of these things on localhost but I plan to use some of them over lan eventually. I attempted reordering the entries, putting the wildcard alias last but that didn't change anything. I'm most likely doing it wrong, but I'm open to suggestions.
Related
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.
I have been trying to get symfony to run, next to a directadmin install. However, after trying several solutions, I have no clue how I can fix the httpd.conf file to get it to work. I'm only getting "Apache is functioning normally" right now.
Would anyone have an idea as to what my httpd.conf should look like?
I created the httpd.conf file by setting up a custom template for directadmin, basically working from what directadmin normally supplies by itself.
This is what the part for this subdomain looks like (IP/Domain have been hidden):
<VirtualHost IP:443 >
SSLEngine on
SSLCertificateFile /usr/local/directadmin/data/users/admin/domains/DOMAIN.cert
SSLCertificateKeyFile /usr/local/directadmin/data/users/admin/domains/DOMAIN.key
SSLCACertificateFile /usr/local/directadmin/data/users/admin/domains/DOMAIN.cacert
ServerName www.sym.DOMAIN
ServerAlias www.sym.DOMAIN sym.DOMAIN
ServerAdmin webmaster#DOMAIN
DocumentRoot /home/admin/domains/DOMAIN/private_html/sym/web
ScriptAlias /cgi-bin/ /home/admin/domains/DOMAIN/public_html/sym/cgi-bin/
UseCanonicalName OFF
<IfModule !mod_ruid2.c>
SuexecUserGroup admin admin
</IfModule>
CustomLog /var/log/httpd/domains/DOMAIN.sym.bytes bytes
CustomLog /var/log/httpd/domains/DOMAIN.sym.log combined
ErrorLog /var/log/httpd/domains/DOMAIN.sym.error.log
<FilesMatch "\.(inc|php|phtml|phps|php70)$">
AddHandler "proxy:unix:/usr/local/php70/sockets/admin.sock|fcgi://localhost" .php70
</FilesMatch>
<Directory /home/admin/domains/DOMAIN/private_html/sym/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
</VirtualHost>
The differences in here compared to a default subdomain generated by directadmin, are the addition of /web behind the documentroot, and the complete "directory" settings.
When I run api.troop37bsa.org/user, I am able to access the page but when I run https://api.troop37bsa.org/user, I get a 404 error. When I run api.troop37bsa.org/?url=user or the secure version I get the correct page.
I am using a .htaccess file to rewrite the url so that everything after the subdomain is considered a parameter string.
So my question is two fold. How can I get https://api.troop37bsa.org/user to work and also what can I do to reduce the size of my .conf file and reduce or merge my .htaccess file.
I have an Apache Server 2.4.18.
My .conf file is setup as follows:
<VirtualHost *:80>
ServerAdmin kreichner#troop37bsa.org
ServerName api.troop37bsa.org
ServerAlias api.troop37bsa.org
DocumentRoot /var/www/troop37bsa.org/api/public
<Directory /var/www/troop37bsa.org/api/public>
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
AccessFileName .htaccess
ErrorLog /var/www/troop37bsa.org/api/public/apache_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =api.troop37bsa.org
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin kreichner#troop37bsa.org
ServerName api.troop37bsa.org
ServerAlias api.troop37bsa.org
DocumentRoot /var/www/troop37bsa.org/api/public
<Directory /var/www/troop37bsa.org/api/public>
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
AccessFileName .htaccess
ErrorLog /var/www/troop37bsa.org/api/public/apache_https_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
In my subdomain folder (ie /var/www/troop37bsa.org/api/public) I have an .htaccess file
ReWriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.+)$ %2index.php?url=$1 [QSA,L]
I am also using a letsencrypt SSL certificate that otherwise seems to be working fine.
Thanks
As I said in the comment section, It turns out that there was a conf file generated by letsencrypt, but it was pointing to the wrong directory publicl instead of public. When I changed the directory name. the .htaccess file did its job. I also removed the second *.443 virtualHost from my orginal conf to avoid further conflicts.
So others may not fall into the same problem the name of the conf was api.troop37bsa.org-le-ssl.conf. This set up the look ups from the SSL certificate.
the api.troop37bsa.org-le-ssl.conf code is as follows:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin kreichner#troop37bsa.org
ServerName api.troop37bsa.org
ServerAlias api.troop37bsa.org
DocumentRoot /var/www/troop37bsa.org/api/public
<Directory /var/www/troop37bsa.org/api/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/troop37bsa.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/troop37bsa.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
Thank you everyone for your help.
In my /var/www I have a number of sites (goodsite, badsite, uglysite). Right now they are accessed by mydomain.com/goodsite, etc..
What I want is for one site in particuar, uglysite, to be accessed by uglysite.mydomain.com - the others remain as they are.
I have tried all sorts of ways of fiddling with the.htaccess (in /var/www). Note I have mod-rewrite enabled and mod vhost-alias enabled.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^uglysite\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/uglysite/
RewriteRule ^(.*)$ /uglysite/$1 [L]
What ends up happening is that both mydomain.com and uglysite.mydomain.com always map to the same thing (i.e., the index at /var/www). I tried adding in a new virtual host, and was surprised to find that uglysite.mydomain.com mapped correctly, but then mydomain.com also mapped directly to uglysite as well.
<Virtualhost uglysite.mydomain.com:80>
ServerName uglysite.mydomain.com
ServerAdmin www#localhost
DocumentRoot "/var/www/"
AccessFileName .htaccess
<Directory "/var/www/uglysite">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
The above was added to my sites-enabled/000-default file. This got uglysite.mydomain.com to work properly, but then mydomain.com mapped to the same thing!
Is there a straightforward way to do what I'm intending to do?? Thanks in advance.
You should be making uglysite into a second file instead of modifying 000-default
So, take a copy of the 000-default file, change the subdomain as you have done up there and modify the directory to /path/to/site
000-default:
<Virtualhost *:80>
ServerName mydomain.com
ServerAdmin www#localhost
ServerAlias mydomain.com
DocumentRoot "/var/www/goodsite"
AccessFileName .htaccess
<Directory "/var/www/goodiste">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
uglysite:
<Virtualhost *:80>
ServerName uglysite.mydomain.com
ServerAlias uglysite.mydomain.com
ServerAdmin www#localhost
DocumentRoot "/var/www/uglysite"
AccessFileName .htaccess
<Directory "/var/www/uglysite">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
Also note that in the above samples, I have modified the DocumentRoot to point to the directory that you want file served from
EDIT: virtualhosts set to *:80 since your sites point to your own ip anyway
I am running Apache2 with multiple vhosts in the sites-enabled folder, each looks a bit like this:
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
DocumentRoot /home/sites/site1/www/
<Directory /home/sites/site/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory /home/sites/mainsite/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
# Provide default favicon.ico and robots.txt using rewrite
RewriteOptions Inherit
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^/(robots\.txt|favicon\.ico)$ /mainsite_alias$0 [PT,NC,L]
</VirtualHost>
The Rewrite code is common to all vhosts (providing a default favicon.ico/robots.txt) but putting this code into httpd.conf does not seem to have any effect - the rewrite logs show nothing.
How can I make this code common to all vhosts without duplicating the code?
Alternatively - is there a better way of achieving default favicons?
Thanks
You could try a AliasMatch:
AliasMatch ^/(robots\.txt|favicon.ico)$ /home/sites/site1/www/mainsite_alias$0
I am using this
<VirtualHost *:80>
ServerAdmin email_at_isp
DocumentRoot "C:/apache/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin_at_server_one.com
DocumentRoot "C:/apache/htdocs/server_one.com"
ServerName server_one.com
ErrorLog "logs/server_one.com-error.log"
CustomLog "logs/server_one.com-access.log" common
</VirtualHost>
and I keep rewrite scripts in .htaccess at the root[C:/apache/htdocs]
I hope it helps somehow, if not enoying. lol