Changing the ServerAlias gives me an Index Of Page - .htaccess

I'm a bit of a newbie when it comes to Server related stuffs, but basically I'm having the most frustrating problem right now...
My configuration file before being changed:
<VirtualHost *:80>
ServerAdmin xyz#gmail.com
DocumentRoot /var/www/xyz.net/public_html
ServerName www.xyz.net
ServerAlias xyz.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory "/var/www">
AllowOverride All
Order allow,deny
allow from all
</Directory>
Which is great, and it sort of worked. Although, going to www.xyz.net would load and you can see the content, however, going to xyz.net would show me an Index Of page. So I changed the config file, just switching the name and alias:
<VirtualHost *:80>
ServerAdmin xyz#gmail.com
DocumentRoot /var/www/xyz.net/public_html
ServerName xyz.net
ServerAlias www.xyz.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory "/var/www">
AllowOverride All
Order allow,deny
allow from all
</Directory>
However, this doesn't seem to work, how come I'm still getting an Index Of page? Why is it that www.xyz.net has all the page content, yet xyz.net doesn't?! I'm really confused.
I've been trying to get www to redirect to non-www, at first I presumed it was an error with my .htaccess file, but after trying various methods of redirecting and even doing it in PHP, I presumed it was Apache screwing me over. So here's the code I use for the htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [L,R=301]
edit:
Thanks for all the help, but everything loads fine on www.xyz.net, however on xyz.net an Index Of page is shown, and there are no indexed items in the list, so presumably it's just an empty web page.

Check if there is somewhere another virtualhost with ServerName: xyz.net. In such case, that other virtualhost can take precedence over yours virtualhost.
Under Linux based systems, there is a default config file which can contain this ServerName. By default, it's location is /etc/apache2/sites-available/default. The file can be also named 000-default.conf

Related

Getting 404 on a specific path /pricing with Apache server [migrated]

This question was migrated from Stack Overflow because it can be answered on Server Fault.
Migrated 2 days ago.
I've set up an Apache server on Ubuntu 20.04
The site loads fine when I load the home page first (https://leadzilla.ai) and after that when I click on the pricing button and it takes me to https://leadzilla.ai/pricing and the that page loads fine as well.
But when I go directly to https://leadzilla.ai/pricing in the browser, I get a 404
Here is what I have in /etc/apache2/sites-available/leadzilla.ai.conf
<VirtualHost *:80>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =leadzilla.ai [OR]
RewriteCond %{SERVER_NAME} =www.leadzilla.ai
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I have this config in /etc/apache2/sites-available/leadzilla.ai-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
</Directory>
<Directory /var/www/leadzilla.ai/blog>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/leadzilla.ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/leadzilla.ai/privkey.pem
</VirtualHost>
</IfModule>
I have Wordpress on https://leadzilla.ai/blog so that has to be taken care of as well.
Here is what I have tried:
RewriteRule ^pricing$ pricing.html [NC]
I added it before the other rewrite rule but that doesn't seem to be working. Any ideas?
[EDIT]
This is solved now. The issue was a Next.js config, not an Apache config.
I put in exportTrailingSlash: true in my module.exports and it worked
I find this to be curious behavior. But if you have a RewriteRule in the <VirtualHost *:443>, then you should also have RewriteEngine On.
Are there any symbolic links in your directory at all. Anything like foo -> foo.html?
Are there any directories like /var/www/html/pricing/ in your directory structure?
Also, remember, that all of your traffic ends up on HTTPS, which means that only the <VirtualHost *:443> is in play. The other virtual host entry only is used long enough to redirect from HTTP to HTTPS. Any rewrite rules for the :80 VirtualHost do not apply on HTTPS.

Running two Flask apps on two different domains using apache2 and WSGI

I have two python (3.6) Flask apps running on Ubuntu 18.04 and I am trying to use Apache2 (v2.4.29) to serve these two apps to two different domains – app1domain.com and app2domain.com. I have two .conf files that I have been trying to modify to get this to work. They currently look like this (replace app1 with app2 for the second one):
WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
WSGIScriptAlias / /var/www/app1/app.wsgi
WSGIProcessGroup app1
WSGIApplicationGroup %{GLOBAL}
<VirtualHost *:80>
ServerAdmin myemail#outlook.com
ServerName app1domain.com
ServerAlias www.app1domain.com
<Directory /var/www/app1>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app1domain.com [OR]
RewriteCond %{SERVER_NAME} =www.app1domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName app1domain.com
ServerAlias www.app1domain.com
ServerAdmin myemail#outlook.com
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Here is where I get stuck, and here is what I have tried:
If both conf files are in the format shown above, app1 will be served to both https://app1domain.com and https://app2domain.com, and app2 will not be shown.
If I disable the conf for app1, app2 is served to both https://app1domain.com and https://app2domain.com which suggests that, at a minimum, both conf files 'work' and the apps are working correctly.
From my investigations, I see a lot of conf files have the WSGI instructions inside the <VirtualHost> tags. If I do this for both confs, the default Apache2 page is displayed on both domains.
I have tried just about every combination of the WSGI instructions inside and outside the <VirtualHost> tag and also the nested <Directory> tag. Most of the just resulting in the default apache2 page.
Am I missing some other option that I need to change? What am I doing wrong here?
I have also been looking for some good documentation on how to interpret these conf files, what the options actually do, so would love if someone could point me to something, particularly if it covers WSGI.
Seems so obvious in hind sight. Turns out the issue was a result of me blindly trusting certbot to autogenerate the new VirtualHost and redirect, and me not really thinking about how this was working.
If you look at the example .conf file in the question, the reason it works is because the WSGI instructions are created outside the scope of the <VirtualHost> tags, which allows them to get picked up by both VirtualHosts. But at the same time, because they are created globally, the WSGI instructions in the .conf file that comes first alphabetically override the others, hence app1 shows up on app1domain.com and app2domain.com.
When I moved the WSGI instructions inside the <VirtualHost> tags, I was moving it inside the <VirtualHost *:80> tags, because all the examples I found were doing that (because they weren't using SSL). When I did that, instead of running the app, the RewriteEngine was redirecting the request to the https version of the website. That gets picked up by <VirtualHost *:443> where you will notice I had no instructions on how to run the app, so we get a default page.
In the end I rewrote my .conf files as follows:
<VirtualHost *:80>
ServerAdmin myemail#outlook.com
ServerName app1domain.com
ServerAlias www.app1domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app1domain.com [OR]
RewriteCond %{SERVER_NAME} =www.app1domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName app1domain.com
ServerAlias www.app1domain.com
ServerAdmin myemail#outlook.com
WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
WSGIScriptAlias / /var/www/app1/app.wsgi
<Directory /var/www/app1>
WSGIProcessGroup app1
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem
</VirtualHost>

You configured HTTPS(443) on the standard HTTP(80) port

In my apache error logs, I have bunch of ssl warnings saving You configured HTTPS(443) on the standard HTTP(80) port!
Here is my site.ca.conf file
<VirtualHost *:80>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.ca
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
and here is my site.ca-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
ServerAlias site.ca
SSLCertificateFile /etc/letsencrypt/live/site.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.ca/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Every thing works fine. I am not sure why this warning shows up every day in my apache log files, and how can I resolve it?
Generally, this is because you do not have any SSL configuration on the virtual host on port 443. You may need to enable "SSLEngine on" and provide certificate information. The warning indicates are serving regular HTTP traffic on what is usually an HTTPS port.
In these config files listed by the requester, there are some tweaks/corrections to be done:
we don't need to have a DocumentRoot in the site.ca.conf because we will redirect HTTP to the HTTPS (site.ca-le-ssl.conf)
the ServerName directive shouldn't have a port number, instead it should be in the VirtualHost level
the ServerName and ServerAlias should be near to each other and there should be www.site.ca as an Alias too in both files to handle the requests containing the www
in site.ca-le-ssl.conf file there is a ServerName site.ca:80 and that's not correct (there should be no port number)
there must be a SSLEngine on in the site.ca-le-ssl.conf
I hope that help someone even this is an old question

Configuring symfony and apache

I'm learning symfony right now. I want to use apache webserver instead of the shipped php. How can I add a virtualhost if I want to use also the default localhost? (localhost:80 for my notebook and I have a "personalhomepage")
Here is one of the vHosts I am currently using (cronolog is used for log rotation):
<VirtualHost *:80>
ServerName projectname.dev
DocumentRoot /var/www/projectname/html/web
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/error_%d_%m_%Y.log"
CustomLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/access_%d_%m_%Y.log" combined
</VirtualHost>
For testing and development purposes I usually create a local domain with the .dev or .local ending in the hosts file, e.g.:
127.0.0.1 projectname.dev
Make sure the domain name corresponds with the one in the vHost config. Oh, and don't dont forget to restart apache. Hope this helps.
Using these steps, you can use either projectname.dev, projectname.dev/app_dev.php or you set the vHost's DirectoryIndex directly, if you want to always use app_dev.php:
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex app_dev.php
</Directory>
You would setup Apache in the same way as any other webservers that have more than one site on them - ideally, a named vhost. Symfony has an example Apache vhost config,
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
ServerAlias www.sitename.127.1.0.1.xip.name
DocumentRoot /var/www/project/web
<Directory /var/www/project/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]
# In local development, I default this to app_dev.php
</IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
In this example, domain.tld (or www.) is the name you would use to reach the site. You would have to be able to reach those names via DNS though - on your local machine, 127.0.0.1. There are also some 'wildcard DNS' services that may help, such as http://xip.name/ With services such as these, you could put ServerName sitename.127.1.0.1.xip.name into the above configuration and then use that to reach your site.
I do something similar myself with a wildcard DNS sub-domain I own for a local machine. I have a number of such Apache Vhost configurations setup.

Apache2, re-direct blank.example.com to example.com/blank/blank

I'm having issues with this re-direct. I've tried the .htaccess method with RewriteRule and RewriteCond, as well as the VirtualDirectory method. Here's what I've tried for VirtualHost:
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /the/doc/root
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
and this for .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank [R=301,L]
I'm not sure if it's needed or not, but blank.example.com is also pointed to localhost in /etc/hosts
Any tips on what I'm missing?
Here are a couple of things.
If you are wanting to just have a subdomain with it's own document root then your vhost needs to look like this. Then there is no need to redirect.
<VirtualHost *:80>
ServerName blank.example.com
DocumentRoot /path/to/blank/blank
<Directory /path/to/blank/blank>
#enable .htaccess for this subdomain
AllowOverride All
</Directory>
</VirtualHost>
If you are truly wanting to redirect your subdomain to the main domain.
Then remove this first vhost. it's not needed at all.
#This virtualhost is not needed
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
Then on your main VirtualHost add a server alias like so
ServerName example.com
ServerAlias blank.example.com
ServerAdmin webmaster#localhost
DocumentRoot /the/doc/root
<Directory /the/doc/root>
#enable .htaccess for this main domain
AllowOverride All
</Directory>
Then for your .htaccess rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank/$1 [R=301,L]
Restart Apache after all config file changes.

Resources