How to add a subpath to root url on Apache - apache

I'm using Apache 2.4 (on a Drupal 7/Docker project).
I want my all my site pages to map from /var/www/html to http://domain/foo/ (eg: /var/www/html/1/2/3/ to http://domain/foo/1/2/3/)
I prefer to avoid using .htacess and only apache conf file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ServerName foo_name
# Try 1
Alias /foo /var/www/html
# Try 2
Redirect "/" "/foo/"
<Directory /var/www/html>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
AllowOverride None
Order Allow,Deny
Allow from All
# Try 3
RewriteEngine On
RewriteBase /foo
To do so I tried :
Alias : Alias /foo /var/www/html so I can access to my front page with http://domain/foo/ but it doesn't work on http://domain/foo/1/2/3/). And also it's just a one way redirection.
Redirect Redirect "/" "/foo/" : I did not worked on my bowser I try to acces to http://domain/foo/ but I got this url (almost infinite loop) http://domain/foo/foo/foo/foo/foo/foo/foo/foo/foo/...
RewriteBase : RewriteBase /foo but it doesn't work probably because I need to match it with htacess on subdirectory.
Most advanced rewriting : I saw a lot of them on StackOverflock/ServerFault but it did'nt seem appropriate for my problem (I never use before regex PCRE, harder to adapt). Moreover I should avoid using rewrite for that case.

It's working like that (alias + RewriteRule) :
Alias /foo /var/www/html
<Directory /var/www/html>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
AllowOverride None
Order Allow,Deny
Allow from All
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Related

Not able to access laravel routes in apache2 server

I've deployed a new laravel installation to my server, While doing so I configured my apache2 as following:
I added 000-default.conf file in /etc/apache2/sites-available/ as following:
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
I've my laravel installed into /var/www/html/stellar folder now I access my installation through:
http://52.39.175.55/stellar/public/
But while calling the routes it is not working, Like
http://52.39.175.55/stellar/public/oauth/token
Here is the screenshot:
But suppose I call through this:
http://52.39.175.55/stellar/public/index.php/oauth/token
I get the access,
I tried changing my public/.htaccess file to something like this:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /stellar
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Still there is no change, Help me out in this.
Your VirtualHost on /etc/apache2/sites-available/ should look like this:
<VirtualHost *:80>
DocumentRoot "/var/www/html/stellar/public"
ServerName yourdomain.com
<Directory /var/www/html/stellar/public>
AllowOverride All
</Directory>
</VirtualHost>
Then restart apache2 and it should work.
I hardly recommend you to duplicate the 000-default.conf file, rename it and include the VirtualHost, then enable it with a2ensite command, just because it's more easy to manage.

DirectorySlash Off doesn't work

I'm using apache 2.4.7
This is my virtualhost's config:
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
In my /var/www/login directory, I have a directory called freeseat and in there, there's the index.php file, so it's full location is /var/www/login/freeseat/index.php
When I try to access it through the following link (domain replaced), it redirects to the same URL with a trailing slash:
http://login.mydomain.com/freeseat -> http://login.mydomain.com/freeseat/
Why isn't the DirectorySlash Off working? I tried to put it in .htaccess, but that didn't help either.
Thanks a lot for your help,
David
First of all, you vHost won't work if you don't use the directive.
This works:
<VirtualHost *:80>
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
Allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
ErrorLog /var/www/login/error.log
</VirtualHost>
To answer you question: DirectorySlash works as expected.
Also see apache documentation for mod_dir, especially the red box called "Security Warning" of section "DirectorySlash".
Tip: always use not publicly accessible error logs to determine the root of evil. :)
Edit #1:
I think I might have misunderstood your question. You want to access /var/www/login/freeseat/index.php through http://login.mydomain.com/freeseat (w/o trailing slash).
Then, drop the freeseat folder and use index.php as var/www/login/freeseat.php and create a RewriteRule in the /var/www/login/.htaccess file (mod_rewrite must be active):
RewriteEngine On
RewriteRule ^freeseat$ freeseat.php
Also, drop
Options +Indexes
DirectoryIndex index.html index.php
from your vHost configuration. It isn't needed anymore.

Localhost subdomains and wildcard issues

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.

"RewriteEngine On" in virtual host not working

This is my virtual host entry.
Version: Apache/2.2.15 (Unix)
<VirtualHost *:80>
ServerName web1.net
ServerAlias www.web1.net
ServerAdmin webmaster#web1.net
DirectoryIndex index.html index.php
LogLevel warn
ErrorLog /var/log/httpd/web1.net_error_log
CustomLog /var/log/httpd/web1.net_access_log combined
DocumentRoot /web/web1.net/htdocs
<Directory "/web/web1.net/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
AllowOverride All
RewriteEngine On #This is not working
</Directory>
</VirtualHost>
.htaccess file
RewriteEngine on # It works.
#abc5
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule .* http://www.example.com
When I put the same (RewriteEngine on) in .htaccess file it starts working, why I need require to put this in .htaccess when it is present in virtual host.
Do I need any other changes in Apache config?
You need to have RewriteEngine on in each directory's .htaccess if you want to enable rewriting. So in short:
RewriteEngine on from server config doesn't come into effect for DocumentRoot/.htaccess
RewriteEngine on in DocumentRoot/.htaccess doesn't come into effect for any /subdir/.htaccess

Non-existing file/URL returns 403 Forbidden

Going to example.com/config or example.com/account/login returns 403 Forbidden.
In reality, /config and /account/login should be redirected to index.php but it gives 403.
My directory structure is like this:
/var/www/example
└─/assets
└─/bower_components
└─/node_modules
└─/partials
└─/templates
└─/tests
└─/vendor
└─index.php
This is my Virtual host configuration:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
<DirectoryMatch "^/var/www/example/(?!(assets|partials))\w+">
Require all denied
</DirectoryMatch>
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Location>
</VirtualHost>
I want to disallow access to every folder except assets and partials, that's why I added that DirectoryMatch directive.
I use a PHP framework called Slim so I have to add Location and Rewrite directives.
I think Apache thinks my URLs are directories and blocks them. Is there a way to unblock my URLs?
Try adding:
<Directory "/var/www/example">
Order Allow,Deny
Allow From all
</Directory>
above the <DirectoryMatch> container in your vhost config.
Since you're using apache 2.4, then use require all:
<Directory "/var/www/example">
Require all granted
</Directory>
If you're using Apache 2.4 try to add in the end of your VirtualHost:
<Directory /var/www/example>
Options Indexes FollowSymLinks MultiViews
# If you want to enable overrides, you should read:
# http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
AllowOverride All
Require all granted
Satisfy Any
Order allow,deny
Allow from all
</Directory>