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');
Related
I currently have a variable host setup using dnsmasq on OS X 10.14. My base folder is ~/Sites. I can have a folder ~/Sites/{whatever} which I can load via http://{whatever}.test/. I am trying to use mod_rewrite for one of my projects, but RewriteEngine is not allowed for some reason.
I have my httpd-vhosts.conf file setup like so:
<VirtualHost *:80>
VirtualDocumentRoot "/Users/{Username}/Sites/%1"
ServerName sites.test
ServerAlias *.test
Options Indexes FollowSymLinks
<Directory "/Users/{Username}/Sites/%1">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and my .htaccess file like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^read$ read.php
RewriteRule ^read/$ read.php
RewriteRule ^read/([\w\s]+)$ read.php?s=$1
RewriteRule ^read/([\w\s]+)/$ read.php?s=$1
RewriteRule ^read/([\w\s]+)/(\d+) read.php?s=$1&p=$2
It doesn't matter what I have in my .htaccess file, as long as RewriteEngine On is there it errors out.
While using %1 in my Directory tag I get the following error:
/Users/{Username}/Sites/reader/.htaccess: RewriteEngine not allowed here
I can't seem to find a way around this without hard coding the directory, but that defeats the purpose of the variable host setup.
If I do hard code the directory the site works just like it should - so everything else is working.
Solved
I managed to figure it out.
In my httpd.conf I had this:
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
Notice the commenting out.
I changed it to this:
<Directory />
AllowOverride All
Require all granted
</Directory>
and it now works.
<Directory "/Users/{Username}/Sites/%1">
%1 is not valid syntax here. I don't think you necessarily need to be specific, as the directory being accessed is already controlled by the (virtual) document root. So, you could, in theory use the <DirectoryMatch> directive instead and providing it matches the appropriate directory pattern it would be sufficient.
HOWEVER, the AllowOverride directive is not permitted in <DirectoryMatch> containers, only in non-regex <Directory> containers, so this would restrict you to do something like the following as a workaround:
<Directory "/Users/{Username}/Sites">
AllowOverride All
</Directory>
<DirectoryMatch "^/Users/{Username}/Sites/[a-z]+">
Require all granted
</DirectoryMatch>
[a-z]+ will match "reader".
UPDATE:
I changed it to this:
<Directory />
AllowOverride All
Require all granted
</Directory>
and it now works.
This enables access to the entire server - which is undesirable (and the Apache docs specifically warn against doing this for security and performance reasons.)
You should be as restrictive as possible - as above (which is the idea behind using the restrictive <Directory> container inside the vHost in the first place).
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
I am getting a weird issue i created a vhost in lamp stack. The problem is that when I open the vhost site www.domain.com the home page loads without any issue.
But when I use the nav bar to open another page I get a "404 NOT FOUND" i.e www.domain.com/about
But as soon as I put the extension of the file manually the page loads.
www.domain.com/about.php
How can I solve this issue. I am using .htaccess to hide the extension of the file.
Note:
1) All the other local file running properly i.e. I have a wordpress site that works fine (This means the mysql db is not causing the error)
2) The vhost is set properly bcus the terminal did not show any error when the vhost was enabled.
But the vhost in wamp does not give me any kind of issue in my windows 8.1 pro.
Edit :
Code in .htaccess to hide extension this works in my windows pc without any problem.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
EDIT -2
<VirtualHost *:80>
<Directory /var/www/stab-website>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
DocumentRoot "/var/www/stab-website"
ServerName stab-site.com
ServerAlias www.stab-site.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Still did not worked.
You need to enable the MultiViews options:
<VirtualHost *:80>
<Directory /var/www/htdocs>
Options Indexes FollowSymLinks MultiViews
</Directory>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/var/www/htdocs"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
</VirtualHost>
This can either be done in your httpd.conf file, in the configuration file for your virtual host or in a .htaccess file.
You should also make sure so that the mod_negotiation module is enabled. This can be done by running:
sudo a2enmod negotiation
You also might need to verify that your virtual host configuration is fine, this can be done from the terminal using:
apache2ctl -t
After this you need to restart your server:
sudo service apache2 restart
From the documentation:
The effect of MultiViews is as follows: if the server receives a
request for /some/dir/foo, if /some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the server reads the directory
looking for files named foo.*, and effectively fakes up a type map
which names all those files, assigning them the same media types and
content-encodings it would have if the client had asked for one of
them by name. It then chooses the best match to the client's
requirements.
Thanks #Cyclone for your help
This post helped me to fix this issue
https://askubuntu.com/questions/233046/how-to-give-my-user-permission-to-add-edit-files-on-local-apache-server
The post above help to run Apache server as the logged in user.
To give rights to use .htaccess --
First, you should ensure that your username is included in www-data group. If not, you can add your username as www-data group
sudo adduser $USER www-data
After that, you should change the ownership of /var/www to your username
sudo chown $USER:www-data -R /var/wwws
And also don't forget to add this code in the apache2.conf
<Directory /var/www/ProjectRootDirectory>
AllowOverride All
</Directory>
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.
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?