Apache2. How to work with <Location> and <Directory> together? - configuration

I'm trying to set specified configuration for location in my apache configuration.
But it isn't working for me.
<VirtualHost 127.0.0.1:80>
ServerName site.ru
DocumentRoot /storage/htdocs
<Directory />
Header add Dir 1
</Directory>
<Location ~ "api">
Header add Api 1
</Location>
</VirtualHost>
I need to extend my directory section with my api section.
But I can't see "Api: 1" in headers list.
What's wrong?

The path in the <Directory> directive must match your DocumentRoot. It's relative to the filesystem, not the document root.

Related

How can I remove /public and the name from URLs in Bolt?

I'm working on a small site and I've got a question.
How can I remove the /public and the singular name from url? However for the entries I want to keep the first segment.
How do I do that? Because when I try to rewrite it in the htaccess the assets break (images etc)
You need to point your server to the /public directory. Here is what the official documentation is saying:
Only the 'public' folder needs to be accessible in the browser. After the first installation this folder is named public/ but as you read on, you will see that you can rename it to www/ or whatever your web server requires. To do this, configure your webserver to use the public/ folder as the web root. For more information about this, see the pages on configuring Apache or Nginx.
Source: https://docs.bolt.cm/3.2/installation/install-command-line
The directive for Apaches VirtualHost is (in your example domaincom.conf in /etc/apache/sites-enabled:
<VirtualHost *:80>
DocumentRoot /your-bolt-directory/public
</VirtualHost>
And for Nginx:
location / {
root /your-bolt-directory/public;
}
Use .htaccess file to rewrite your URL's:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/page [L]
</IfModule>
The public part of the url should not be visible if you have your Apache virtualhost setup correctly.
Ordinarily, all you need to do here is point the DocumentRoot setting to the public directory, so for example if your setup currently looks like this:
DocumentRoot /home/mysite
You adjust it to:
DocumentRoot /home/mysite/public
You may also need to do the same for any <Directory /home/mysite > commands that are in your config too.
Once you have amended, restart Apache and http://yourdomain.com/ should load up the homepage and http://yourdomain.com/bolt take you to the backend.

Apache2- need multiple projects with same domain that use different path after domain name

I have 2 completely different projects I would like to host from the same domain name "mysimpledomainname.com" with ip address xx.xx.xxx.xx (not local)
The 2 projects are located at
Project 1) /var/www/html/project1
Project 2) /var/www/html/project2
I would like to have the 2 projects resolve like this
Project 1
mysimpledomainname.com
project 2
mysimpledomainname.com/project2
I DO NOT want to use subdomain names. This is for a project on my public server, not my local machine, so I don't think updating the host file will help me.
I already have the first project loading from mysimpledomainname.com (it's already working).
In the directory sites-available I have the conf file mysimpledomainname.com.conf with values:
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
DocumentRoot /var/www/html/project1
<Directory /var/www/html/project1/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I think I need to have 2 different conf files set up in order to do this, but after searching around I have not been able to find a solution.
Can anyone help me with this? Rather than giving a short answer can you post exactly what I would need for the 2 different conf files?
Thanks,
David
You can have only 1 VirtualHost directive as this is defined per FQDN and you want to use only mysimpledomainname.com.
Your config for Project1 is OK. You just need to make available another directory under path /project2. To do this, you need to use the Alias directive.
So your final config should be something like this (of course you can have separate Directory configurations for each subdir if you want):
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
DocumentRoot /var/www/html/project1
Alias /project2 /var/www/html/project2
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache2 custom 404 Page won't load (.htaccess)

i want to create a custom errorpage for my small webserver which runs on apache2 on raspbian. But sadly the created page isn't loaded by a browser in case of an URL which can't be resolved ... the browser just prints the path to the created errorpage on the upper left-hand corner.
html of the custom page so far:
<!DOCTYPE html><html><head></head><body><p>Nooooot found ...</p></body></html>
html of the loaded page:
<html><head></head><body>sites/errorsites/404.html</bod></html>
My .htaccess-file, which is placed in /var/www/
Options -Indexes
ErrorDocument 404 sites/errorsites/404.html
Location of my errorsites-dir with custom errorsites: /var/www/sites/
(Of course I set "AllowOverride" in the default-file in /etc/apache2/sites-available to "all".)
Thanks for the help
Lukas
Edit:
default-file of apache:
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
From the ErrorDocument docs:
URLs can begin with a slash (/) for local web-paths (relative to the
DocumentRoot), or be a full URL which the client can resolve.
Alternatively, a message can be provided to be displayed by the
browser.
sites/errorsites/404.html does not begin w/ a slash and so is treated as a message to be displayed. Changing it to /sites/errorsites/404.html should fix, if sites is relative to your DocumentRoot…

Treating a subdirectory as a root directory for Apache

I cannot find a way to do what I want. I want to upload a project to my server at http://example.com/MyNewProject
In each of the web pages I include a file which does all of the imports for stylesheets, JavaScript etc. I don't want to provide the full path every time I just want to be able to do /MyStylesheet.css
My main websites document root is /var/www/html/example and my new projects directory is stored within /var/www/html/example/MyNewProject.
What I want is in my import when I do /MyStylesheet.css instead of going to my servers main web directory it will go to /var/www/html/example/MyNewProject in order to get the CSS.
I've tried adding the following to my Apache config file:
Alias /NewProjectTemplate "/var/www/html/example/NewProjectTemplate"
Alias /NewPRojectTemplate/ "/var/www/html/example/NewProjectTemplate"
<Directory "/var/www/html/example/NewProjectTemplate">
Options Indexes FollowSymLinks Includes
AllowOverride All
XBitHack On
Order allow,deny
Allow from all
</Directory>
This unfortunately hasn't worked so I've also tried adding the following to my virtualhosts file
<VirtualHost *:80>
DocumentRoot /var/www/html/example/NewProjectTemplate
ServerName example.com/NewProjectTemplate
</VirtualHost>
I've looked around on Google but cannot find anything specific, the only thing I've found is something to do with using the Rewrite engine but this seems a little too complicated and OTT for my needs.
Update
I've got a little further, although not a lot, I'm sure its not supposed to be this complicated.
Instead of using the virtual hosts, I'm now using an alias and below is what I've added to my httpd.conf file
DocumentRoot "/var/www/html"
Alias /NewTemp/ "/var/www/html/example/NewProjectTemplate/"
Alias /NewTemp "/var/www/html/example/NewProjectTemplate"
<Directory "/var/www/html/example/NewProjectTemplate/">
Options FollowSymLinks Includes
AllowOverride all
</Directory>
<Directory "/var/www/html/example/NewProjectTemplate">
Options FollowSymLinks Includes
AllowOverride all
</Directory>
In my HTML code in my SSI I am doing the following:
<!--#include file="includes/imports.html"-->
As you can see this is a relative path and Includes is inside the root of NewProjectTemplate. However, this is a template file so I want it to always to go to the root to find the file so it is guaranteed to work no matter how deep into the site it. For example if I change the line to the below I then get an error error processing directive
<!--#include file="/includes/imports.html"-->
So even though it works without the / but doesn't with the slash, if I don't include the slash so this import then works, the imports within the imports.html file work even though they do contain the leading slash so it always go to the root. Below is my HTML imports file
<link rel="stylesheet" type="text/css" href="/StyleSheet.css">
<link rel="stylesheet" type="text/css" href="/navigation/top-nav.css">
<link rel="stylesheet" type="text/css" href="/navigation/side-nav.css">
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript" src="/scripts/menu.js"></script>
So just to clarify, if the #include file = includes/imports.html the SSI directive works and /StyleSheet.css is successfully imported even though it has the leading slash. If I add a / to the path of the SSI it then doesn't get imported.
But here is where it gets weirder.
If I then add another file to a subdirectory e.g. NewProjectTemplate/MySubDirectory and then add the SSI to be ../includes/imports.html it still doesn't work. Its like the #include file SSI directive expects the file to be in the same working directory.
So, have you tried like this in the virtual host?
<VirtualHost *:80>
ServerAdmin emailaddress#domain.com
DocumentRoot "/var/www/html/example/"
ServerName example.local
ServerAlias example.local
<Directory "/var/www/html/example/">
Options All Includes Indexes FollowSymLinks
Order allow,deny
Allow from all
AllowOverride All
</Directory>
Alias /NewProjectTemplate /var/www/html/example/NewProjectTemplate
<Directory "/var/www/html/example/NewProjectTemplate">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Hope it helps this time..
Just accidentally stumbled across this very old threat.
No need of host or alias if you just want a sub dir to be treated as root.
The way to do it is to amend the httpd.conf, there you will find
<Directory>
just update this line and add
<Directory "your/server/root/original_path/subpath">
# your options
</Directory>

Files not showing up because looking at root but I think I specify the root!

I'm working locally and I always specify files as /files.jpg or whatnot so the server will always know to look at the root level to find the files. That way if I'm on a subpage it won't choke. But locally the files do not show up. My file structure is apache2/htdocs/name_of_folder. name_of_folder is my "root". I then modify my virtualhost file so when I enter localhost/name_of_folder it will show my site. I specify the DocumentRoot so I thought when I say localhost/name_of_folder that is working at the document root?
In httpd-vhosts.conf I have it set to
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/opt/local/apache2/name_of_folder"
ServerName localhost/name_of_folder
ServerAlias www.localhost/name_of_folder
ErrorLog "logs/name_of_folder_log"
CustomLog "logs/name_of_folder_access_log" common
</VirtualHost>
What am I doing wrong?
You are misusing the ServerName and ServerAlias directives. They have nothing to do with files or paths. They are the domain name that identifies the web site (together with an optional port if not 80). If you want to move your document root, you have to edit the DocumentRoot directive.
You may not do this in that way. The directive ServerName is not supposed to get an URL but a domain name. Set the root to the parent folder, deny all access to it using <Directory> and allow access to you subdirectory to get it work.
In stead of doing:
ServerName localhost/name_of_folder
ServerAlias www.localhost/name_of_folder
Create a subdomain:
ServerName subdomain.localhost