Apache AliasMatch shows content of directory? - apache

What I'm trying to achieve is quite simple. I want Apache to use the content of /home/user/www/bar/public when users go to www.example.com/foo/bar. The bar part may change and I want Apache to be able to change the directory in consequence.
I tried to do this. And it works but it won't load my index.php file and actually shows the content of the directory...
<VirtualHost *:80>
ServerName example.com
AliasMatch ^/foo/(.*)$ /home/user/www/$1/public
<Directory /home/user/www/$1/public>
AllowOverride all
</Directory>
</VirtualHost>
There isn't any .htaccess file and it doesn't need one because I managed to make it work with a subdomain :
<VirtualHost *:80>
ServerName test.example.com
DocumentRoot /home/user/www/test/public
<Directory /home/user/www/test/public>
AllowOverride all
</Directory>
</VirtualHost>
I'm really stuck and any help would be great ! Thank you.

You can't reuse the "$1" in the section because that's local to the AliasMatch.
You can simply use a wildcard if you are okay with applying the contents to every public dir under every subdir of /home/user/www

Related

Redirection to a subfolder

Here is the tree of my project :
/project1
/public
page1.php
page2.php
/project2
...
I would like to access my pages with
/project1/page1.php and not /project1/public/page1.php
But I also want to not have access to pages by going to /project1/public/*
I know that I have to manage this with a .htaccess file or httpd.conf file but after trying all the solutions available on several sites, I still can not get what I want.
Thanks in advance and sorry for my english level.
I just found a solution but I don't know if it's proper :
<VirtualHost *:8888>
ServerName localhost
DocumentRoot /Applications/MAMP/htdocs/project
SetEnv APPLICATION_ENV "dev"
Alias /project /Applications/MAMP/htdocs/project/public
<Directory /Applications/MAMP/htdocs/project/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

placing www infront of my domain displays directory view

I'm sorry if this is elsewhere on SO, but I can't find anything that can help me with my issue.
A while back, I updated my server, which wrecked the LAMP stack. So I installed it all again, but I must have done something wrong.
When I try to enter my domain, with www infront, it displays the directory view.
It's like it totally disregards my .htaccess file.
I have the following file in /etc/apache2/sites-enabled/
<VirtualHost *:80>
{comments}
ServerAdmin webmaster#localhost
ServerName domain.com
ServerAlias domain.com domain.com
DocumentRoot /var/www/html/domain
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
{comments}
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
{comments}
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Besides that, I have the following line in the top (amongst some redirects) of my .htaccess file
Options -Indexes
I have no idea, how to fix this.
I don't want people in the directory view. And unfortunately some browsers automatically place www infront, plus a lot of people write www in front of urls.
Thanks in advance
1- Replace ServerAlias domain.com domain.com by ServerAlias www.domain.com domain.com.
2- Replace AllowOverride None by AllowOverride All in your Apache configuration file.
This way, your .htaccess will be able to override some general configuration primitives, like displaying indexes.
Once it works, modify AllowOverride All to put only what is really needed, for security reasons.
I solved this myself.
I found out that I had a default.conf, that the www trigered.
So changing this to direct to my folder for the domain, worked.
Thanks to #Alexandre Fenyo for the help

Redirect sub.domain.com to /var/www/subfolder and sub.domain.com/api to /var/www/apifolder

I want to redirect the /api folder to a new documentRoot (new virtualhost) from the already existing virtualhost
domain.com -> /var/www/domain
sub.domain.com -> /var/www/sub
sub.domain.com/api -> /var/www/api
And should be recursive, so sub.domain.com/api/v1 should also go to /var/www/api.
Is this even possible with apache?
Any suggestions to make this less confusing would be appreciated ;p
As already said above in the comments you can simply define virtual hosts for this. You do not need any redirection commands at all:
Listen 80
<VirtualHost *:80>
ServerName domain.com
DocumentRoot /var/www/domain
<Directory /var/www/domain>
# ...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.domain.com
Alias /api /var/www/api/index.php
<Directory /var/www/api>
# ...
</Directory>
DocumentRoot /var/www/sub
<Directory /var/www/sub>
# ...
</Directory>
</VirtualHost>
This obviously assumes that your API is implemented by a script called /var/www/api/index.php. You certainly can save that implementation as whatever you like, for example /var/www/api. However that does not look very realistic to me, APIs are typically implemented on a scripting base and scripts usually require include files, so a folder holding the implementation certainly does make sense. And I doubt you want to throw all those files into /var/www, so I assume that /var/www/api is meant to hold the implementation.
I tested this setup locally, it does work for me.

Apache Passenger handle 'www' in url

I've an application deployed to EC2 using passenger and apache2.
I've registered a domain (not with AWS) and have two 'A' records pointing to my instance's elastic IP
appname.com.
*.appname.com.
In my apache2.conf file (below), I've got ServerName appname.com
The application is working perfectly when you type appname.com into a browser. But if you put www.appname.com into the browser, you just get the default apache page.
Does anyone know what I put in ServerName to handle both? I've tried adding another ServerName line under the first but it doesn't work.
Thanks for looking
/etc/apache2/apache2.conf
<IfModule mod_passenger.c>
PassengerRoot /var/lib/gems/1.9.1/gems/passenger-5.0.6
PassengerDefaultRuby /usr/bin/ruby1.9.1
</IfModule>
<VirtualHost *:80>
ServerName appname.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /home/ubuntu/projects/appname/public
ErrorLog /home/ubuntu/projects/appname/log/error.log
RailsEnv development
<Directory /home/ubuntu/projects/appname/public>
# This relaxes Apache security settings.
AllowOverride all
Require all granted
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
I solved it thanks to this post (shame you can't thank on the other stack sites using your stackoverflow reputation)
Under
ServerName appname.com
you put
AliasName www.appname.com
or even better
AliasName *.appname.com

Trying to forward domain.com/abcd to some different local subfolder

In the apache server installation I have a .conf file which forwards all requests from www.domain.com to c:\www\domain. Now, i'm trying to forward www.domain.com/abcd to a different subfolder locally (say c:\www\abcd). i tried adding another .conf which looked like this.
<VirtualHost *:80>
ServerName www.domain.com/abcd
DocumentRoot "c:/www/abcd"
ErrorDocument 404 /404.php
ErrorLog "C:/logs/error.log"
CustomLog "d:/logs/access.log" common
<Directory "c:/www/abcd">
RewriteEngine on
AllowOverride All
</Directory>
<Location />
Order deny,allow
Deny from all
Allow from all
</Location>
</VirtualHost>
This doesn't work as intended. When i go to www.domain.com/abcd , I guess its trying to access c:\www\domain\abcd and showing not found error. Is there anyway to solve this issue without making DNS entries and without making changes to your local hosts file?
One possible solution would be to make a symbolic link
mklink /D C:\www\domain\abcd C:\www\abcd