Default index.html page for any virtual host - apache

I have configured some vhost like:
<VirtualHost *:443>
ServerName test.mywebsite.com
ServerAlias test.mywebsite.com
DocumentRoot /hosting/test
<Directory "/hosting/test">
AllowOverride All
Order Allow,Deny
Allow from All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /usr/local/apache/ssl/mywebsite.com.pem
SSLCertificateKeyFile /usr/local/apache/ssl/mywebsite.com.key
</VirtualHost>
Now i need to display a default webpage if domain root folder not have any index page like index.php or index.html.
Actualy if open wesite it return 403 forbidden if index.html/php is not present.
My idea is;
Folder site no index.html/php show:
defaultwebpage.html in /etc/var/http/hdocs/default.html
How to this?
Apache config.conf is configured with default root but if put the index.html here it is not displayed in case of lack.
DocumentRoot "/data/www/default"
<Directory "/data/www/default">

Couldn't you use the ErrorDocument thing to redirect all 404 to a certain default page? If that default page is outside your webroot use alias to bring it in.
So
Alias /specialindex /etc/var/http/hdocs/
and
ErrorDocument 403 /specialindex/default.html
Looks strange, but should work. But, catches all 403 of course...

The solution:
<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>
<Directory /usr/share/httpd/noindex>
AllowOverride None
Require all granted
</Directory>
Alias /.noindex.html /usr/share/httpd/noindex/index.html

Related

apache 2.4 - deny direct ip access and show custom error page

I want requests to http://example.com to serve my website, and requests to http://123.123.123.123 to show a customized 403 error page.
I have Apache/2.4.29 running on my Ubuntu server.
/etc/apache2/sites-available/vhosts.conf
<VirtualHost *:80>
ServerName default
<Location />
Require all denied
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
/etc/apache2/conf-available/security.conf
Alias /errors/ "/var/www/html/error-pages/"
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
<Directory /var/www/html/error-pages>
Require all granted
</Directory>
With the above setup direct-ip access is blocked, however it is not showing my custom error page.
Forbidden You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to
use an ErrorDocument to handle the request.
Then I tried using the directive instead of :
<VirtualHost *:80>
ServerName default
<Directory />
Require all denied
</Directory>
</VirtualHost>
Now all subdirectories (e.g. http://123.123.123.123/sub) show my error page, but unfortunately the root is not blocked at all and showing my index.php page.
My next guess was to experiment with wilcards:
<VirtualHost *:80>
ServerName default
<Location "/*">
Require all denied
</Location>
<Location "/*/">
Require all denied
</Location>
</VirtualHost>
This code works, and shows my own 403 error page on the root aswell all the subdirectories.
All this got me confused!
Is the how it should be done, or am I doing something wrong in the first two examples?
<VirtualHost _default_:80>
<Directory "${SRVROOT}/htdocs">
Require all denied
</Directory>
ErrorDocument 403 "Sorry, direct IP access not allowed."
ErrorLog "${SRVROOT}/logs/default-http-error.log"
CustomLog "${SRVROOT}/logs/default-http-access.log" combined
</VirtualHost>
This is what I have setup in my server where the directory directive is to denied access to all files in this virtualhost. And the errordocument 403 serving as a message. Other syntax are for error logging. Make sure this virtual host is before any other virtualhost. Something similar for the HTTPS scheme if you support HTTPS on your server.

Symfony - Use Apache ErrorDocument

I want to replace symfony's Error template with apache ErrorDocument.
I have following conf:
<VirtualHost *:80>
ServerName localhost-de
ServerAdmin me#mydomain.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
ErrorDocument 404 /staticErrorPage.html
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /kontakt "/var/www/html/contact/web"
Alias /skin "/var/www/html/skin"
RewriteEngine on
RewriteRule ^/skin/(.{7})/(.*) /skin/$2
SetEnv locale de_DE
</VirtualHost>
My Symfony application is under "/var/www/html/contact/". When i enter a not existing page like "localhost-de/foo" i am getting the content of my html page.
But when i enter a not existing page like "localhost-de/kontakt/foo" i am always getting the content of symfony/twig page.
I know that i can override twig template but i dont want it this way. Any ideas how to handle this?
The ErrorDocument is used only for the Directory you specified. You should enclose ErrorDocument in a Location or LocationMatch directive. Read apache documentation for more details.

Redirect to a subfolder in Apache virtual host file

I have Joomla installed on a webserver running Ubuntu Server 12.04. The Joomla folder is located at /var/www/cms/.
My vhost file at /etc/apache2/sites-enabled/default has the following content:
<VirtualHost *:80>
ServerName domain.com/
Redirect permanent / https://domain.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName domain.com:443
DocumentRoot /var/www/cms
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/cms>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
(...)
</VirtualHost>
At the moment, all the requests to domain.com and anything entered after that like domain.com/example gets directed and processed by Joomla which either redirects to a proper page or returns a custom 404 error. This all works.
Now, I would like to filter all the requests that go to domain.com/subfolder before they get processed by Joomla and redirect them to /var/www/subfolder (instead of my root folder at /var/www/cms/).
I believe the file in /etc/apache2/sites-enabled/default (seen above) is the right place to define such a redirect, however I have not been able to figure out at what position and how to achieve this.
You should add to your configuration:
Alias /subfolder /var/www/subfolder
<Directory /var/www/subfolder>
Order allow,deny
allow from all
</Directory>
and fit the configuration between "Directory" to your needs.
See the Apache documentation to have more informations.

What is wrong with this apache virtualhost directive?

I'm getting 403 forbidden errors with this virtualhost directory in apache 2.2. Can anyone help, please?
<VirtualHost *:80>
ServerName www.xyz.com
ServerAlias xyz.com
DocumentRoot "/home/bruce/projects/links/www"
<Directory />
AllowOverride None
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
DirectoryIndex index.html index.htm index.jsp index.php
</VirtualHost>
Thanks!
EDIT error in log is
(13)Permission denied: access to / denied
If i'm not wrong you have indicated the root directory of you file system in < Directory>
It should be:
<VirtualHost *:80>
ServerName www.xyz.com
ServerAlias xyz.com
DocumentRoot "/home/bruce/projects/links/www"
<Directory /home/bruce/projects/links/www>
...
Gah. It ended up being that I was indeed forbidden from accessing that page. I am using a new virtual server provider, and they have set up my /home account as readable only by me. As a result, the user running apache was unable to view the files. I've changed the permissions on my home directory to be more lenient and now everything works as expected.

.htaccess file can't find 404 ErrorDocument

The .htaccess file I have is unable to find my 404.html document in a sub-folder called "error". The line in particular is:
ErrorDocument 404 /error/404.html
However, if I place 404.html in the same directory as .htaccess and change the line to:
ErrorDocument 404 /404.html --- It works!
I'm using XAMPP for Windows if that helps, and in my httpd.conf file, the relevant directives are set as follows (I realize there is some redundancy):
<Directory />
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI MultiViews
AllowOverride All
Require all granted
</Directory>
Also, my DocumentRoot is set to "C:/xampp/htdocs", and I'v tried various paths to access the "error" directory--none work:
ErrorDocument 404 /error/404.html
ErrorDocument 404 /flchi/error/404.html
ErrorDocument 404 /sites/flchi/error/404.html
ErrorDocument 404 /htdocs/sites/flchi/error/404.html
Oh, and one final thing for what it's worth--I'm using virtual hosts. Here's the relevant bits:
<VirtualHost *>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
ServerAlias localhost
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost flchi.local>
DocumentRoot "C:/xampp/htdocs/sites/flchi"
ServerName flchi.local
ServerAlias flchi.local
<Directory "C:/xampp/htdocs/sites/flchi">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This happens because of naming conflict - default Apache configuration already defines virtual path for /error, affecting all virtual hosts globally. It's done in config file xampp/apache/conf/extra/httpd-multilang-errordoc.conf. Location /error, therefore shadows your /error subdirectory:
Alias /error/ "C:/xampp/apache/error/"
It should work if you rename your 'error' sub-directory to something else - for example, error2.
ErrorDocument 404 /error2/404.html