Mutiple Sites on localhost root (xampp) /w absolute paths - apache

I have several projects on Linux-Servers which I work on locally with my Windows xampp enviroment.
However, the apache directory (Virtual Host) settings are like this on all projects:
DocumentRoot /var/www/html/project1/web
<Directory /var/www/html/project1/web/>
AllowOverride All
Order Allow,Deny
Allow from All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</Directory>
Now I can use absolute paths in my source such as /images/
When I create a Directory in the xampp httpd.conf like this, it works, but only for the project I have the document root specified for:
DocumentRoot "C:/xampp/htdocs/project1/web"
<Directory "C:/xampp/htdocs/project1/web">
....
</Directory>
I would like to work on project2 as well but I also want the /images/ path to work in p2 as well, so changing the DocumentRoot to htdocs does not work. I also tried Alias but to no avail.
Where am I wrong, how do other people work on multiple projects, do they update the https.conf every time the switch projects?

have nothing to test here but you could edit your hosts to something like:
127.0.0.1 localhost
127.0.0.1 foo.localhost
127.0.0.1 bar.localhost
Once you're done, you should be able to create multiple Virtualhosts with different paths

Related

Get 403 error when create multiple vhost outside the XAMMP htdocs

I know this is quite old problem, but i have no luck when trying to find the right solution. I have tried every possible configuration but still no luck. I'm working on PHP project using laravel. I got no problem at all when set up the laravel.labs as vhost INSIDE the htdocs. But when I move it into separated folder which is C:\vhosts i got this 403 error. The purpose of vhosts folder is to hold multiple vhost. The following are my configuration:
System:
windows 7 64-bit
XAMPP v 1.7.7
Apache/2.2.21
PHP: 5.3.8
httpd.conf :
# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"
httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/laravel_labs/public
ServerName laravel.labs
</VirtualHost>
c:\windows\system32\driver\etc\host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 laravel.labs
.htaccess (inside the C:\vhosts\laravel_labs\public)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can anyone help to find something that i missed? any advice guys? thanks
Every time you add a new Alias or DocumentRoot, you need a <directory> section just like the one for your default DocumentRoot. Depending on your release, this either has something like
Order deny,allow
or
require all granted
Finally i found The answer for this problem. I have to add some lines in httpd.conf
Something like this:
Alias /laravel_labs/public "C:/vhosts/laravel_labs/public/"
<Directory "C:/vhosts/laravel_labs/public/">
AllowOverride All
Order Allow,deny
Allow from all
</Directory>
See the detail explanation here :
http://www.youtube.com/watch?v=Z250saioXIs

Inconsistency when switching between development to live server?

I always have trouble when moving a site that I've developed in localhost into my hosting server with the .htaccess file. Specifically with relative paths to the files. It's not hard to fix, but it's quite bothering.
For example:
Whereas ErrorDocument 404 /foo/404.php works in the
hosting, for it to work in localhost I have to specify ErrorDocument
404 /projectroot/foo/404.php.
When using mod_rewrite, RewriteRule ^example/$ example.php [L,NC]
works in localhost, but for it to work in the hosting I must use
RewriteRule ^example/$ /example.php [L,NC] (note the slash before the file name).
It could be because I'm running localhost on Windows and my hosting is on Linux, but I don't think that's the problem.
So my question is, how can I ensure the paths to the files are correct when working locally or on the remote server? What is the "working directory" for the .htaccess file?
This may look slight more complicated then it seems, but I would work out your issue in the following way.
Create a dev domain on the C:\Windows\System32\drivers\etc\hosts file pointing to the IP 127.0.0.1 which is the localhost IP.
The domain name doesn't really matter just something easy for you to use for that given project for example test.dev so the hosts file would look like this:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 test.dev
127.0.0.1 www.test.dev
Now define where the folder for that project will be, I will use c:\projects\test for this example.
Now create on your web server a new virtualhost for the domain you have just created, here is a sample of virtualhost that I use:
<VirtualHost *:80>
ServerAdmin root#localhost
DocumentRoot "C:/projects/test"
ServerName test.dev
ServerAlias www.test.dev
ErrorLog "C:/projects/logs/test.dev_error_log"
CustomLog "C:/projects/logs/test.dev_access_log" common
RewriteLog "C:/projects/logs/test.dev_rewrite_log"
RewriteLogLevel 9
ScriptAlias /cgi-bin/ "C:/projects/test/cgi-bin/"
<Directory "C:/projects/test">
Options -Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/projects/test/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Restart your server to activate the changes, you do not need to restart the computer for the hosts file, it should work immediately once you save it.
Now you can use http://www.test.dev or http://test.dev and you can use the very same .htaccess on both, dev and live sites.
Some other tips:
Always use and define RewriteBase / as it can make a difference specially when you want to use rules without the / at the start, like you have mentioned for RewriteRule ^example/$ example.php [L,NC]
Depending on the rules you want to use, you may need to specify -MultiViews so I often just leave it on by default, like this:
Options +FollowSymLinks -MultiViews
As you can see on my virtualhost setup, I define the access, error and rewrite logs into the logs folder by default and with the domain name in question so when I need to see what is going on I have an easy access with the clear logs for that domain only and not my whole bunch of projects running on the same virualhost.
I guess that is about it, you don't really need that huge virtualhost that's just how I use it and its not like I need to copy and paste it all the time as I have my own simple script to create new domains and do all the work for me.

File not found with VirtualHost and mod_rewrite

I'm bulding a RESTful api based on Tonic.
On my developer machine and our stage server we use virtual hosts.
Tonic uses a .htaccess file to translate the incomming calls to it's dispatcher.php file. This works fine on servers without VirtualHosts enabled.
However if i enable VirtualHosts i get a file not found even thought the path and name to the file is correct.
Here is the VirtualHost setup on my developer machine.
<VirtualHost *:80>
ServerAdmin admin#xxxxxxxxxxxx
ServerAlias *.dev.xxxxx
VirtualDocumentRoot /home/xxxxxxxx/workspace/%1
<Directory /home/xxxxxxxx/workspace/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And Tonic's .htacces located in a folder called rest in the project root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !dispatch\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* dispatch.php [L,QSA]
</IfModule>
A call to http://project.dev.xxxxx/rest/ gives:
Not Found
The requested URL /home/xxxxxxxx/workspace/project/rest/dispatch.php was
not found on this server.
Apache/2.2.22 (Ubuntu) Server at xxxxxxx Port 80
It appears as though you're misusing VirtualDocumentRoot. Try changing it to:
DocumentRoot /home/xxxxxxxx/workspace/project/rest
Also, here's a good explanation on the VirtualDynamicRoot: Dynamically configured mass virtual hosting
Hope that helps.

apache2 httpd configuration

My document root is /var/www and I have no virtual hosts enabled.
This is my folder structure of /var/www:
index.php
classes (external)
controllers
models
files (img, js, css)
views (pages, components)
As you can see I am using a model view controller pattern. What I need now is the correct configuration I have to use in my httpd.conf to define that only the files folder can be accesed and no other folder, to prevent "Not found" messages or direct php access. How can I set this up?
This is my current httpd.conf
ServerSignature Off
ServerTokens Full
# Settings for server # port 80.
<VirtualHost *:80>
ServerName <url>
DocumentRoot /var/www
DirectoryIndex index.php
# No one has access to the main directory.
<Directory />
Order Deny,Allow
Deny from all
Options None
AllowOverride None
</Directory>
# Configure the main directory
<Directory /var/www>
# Everyone has access to the main directory.
Order Allow,Deny
Allow from all
Options FollowSymLinks
AllowOverride None
# Enable clean urls.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>
</VirtualHost>
Thanks for help :)
If possible, it would be ideal to keep your controllers, view scripts and other application related code out of /var/www and instead put it in /var/application or something like that.
Then you don't need any rewrite rules to deny access to everything but files. If you ever wanted to add access to a new folder (e.g. /var/www/css) then you will likely have to do something to make it accessible. Or you have the reverse situation where you explicitly deny the folders you don't want accessed. That works but if .htaccess is ever broken or someone forgets the rules moving to a new server then you have more work to do.
In index.php, define some constant that tells where the files live (e.g. define('APPLICATION_PATH', '/var/application');

Modify htaccess file for two sites

My web host points my "main" domain name to the root www folder. The web files for that site are located in the "www/app/webroot" folder. I currently have the site up and running using the following in the htaccess file:
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
I'm trying to start a dev site for the same site. I made a folder named "dev" in the www folder. So, the web files for this folder are in: "www/dev/app/webroot" I have a sub-domain pointing to the dev folder. When I use the same htaccess as above in the dev folder, it doesn't work because (I believe) it is inheriting the settings from the root www folder. When the page loads, it just comes up blank. How do I set up my htaccess files to allow for both sites?
Thanks in advance for any help! I'm obviously a novice at this stuff.
So we'll try to clean the things :-)
Avoid using .htaccess. All the settings in a .htaccess in a directory /foo/bar can be set in apache configuration as a Directory setting (.haccess is usefull if you provide limited access on apache conf, if you own the server don't use it).
<Directory /foo/bar>(...)</Directory>
Then you can access your sites with named based virtualhosts. Verify you have this option:
NameVirtualHost *:80
When you have it nice things can start.
This will be your virtualhost for your 1st app:
<VirtualHost *:80>
ServerName app
ServerAlias www.app.somwhere.com
ServerAlias app.somwhere.com
DocumentRoot /www/app/webroot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/app/webroot>
Options Indexes FollowSymLinks
# this prevent.htaccess reading, remove if you want .htaccess
AllowOverride None
# allow web access
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Most apache settings can be define here. Only for your 1st app. Apache will serve this configuration for all requests done for the site name 'app', or 'www.app.somwhere.com', or 'app.somwhere.com'. You can define a lot of alias(ServerAlias)., and only one name (ServerName).
Then if you go in your browser and type http://app/ your browser won't find the server, so set it in your /etc/hosts. This is what every people wanting to access your app should have in the hosts file until you get a real DNS (assuming your 1st app is app.somwhere.com and the second foo.somwhere.com and 92.128.52.226is your external IP):
127.0.0.1 app.somwhere.com app foo foo.somewhere.com
92.128.52.226 app.somwhere.com app foo foo.somewhere.com
And now let's add another virtualhost for your second app:
<VirtualHost *:80>
ServerName foo
ServerAlias www.foo.somwhere.com
ServerAlias foo.somwhere.com
DocumentRoot /www/foo/webroot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/foo/webroot>
Options Indexes FollowSymLinks
# this prevent.htaccess reading, remove if you want .htaccess
AllowOverride None
# allow web access
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And etc.
Don't forget to restart your apache. No rewrite rule. nice virtualhosts is the 1st step of a nice configuration, you will be able to define rules, directory or location specific things per name used. Even php configuration can be set per virtualhost with php_value instead of a global shared one on php.ini.
type
apache2 -S
to get the list of your virtualhosts, you'll see that the first one is the 'default' one, if apache does'nt understand the name of the requested site it will serve this default one (so you could ad a specific virtualhost on top to handle theses cases).
Try adding dev/ to the paths in lines 3 and 4 to your dev .htaccess.
Maybe you should remove the "RewriteBase /" line in the .htaccess in your dev folder?