I'm trying to create a RESTful API on a VirtualHost on my Apache 2.4 server (on Ubuntu). I have a php file named dbManager.php which I am using a RewriteRule to look like an api directory. It's working great except for PUT and DELETE commands, which are returning 403 errors. Here's a redacted version of my conf file:
<VirtualHost *>
ServerAdmin onigame#gmail.com
ServerName servername.com
ServerAlias *.servername.com
DirectoryIndex index.html index.php
DocumentRoot /path/to/local/dir/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
<Limit PUT DELETE>
Require all granted
</Limit>
</Directory>
# RESTful services provided for the fake "api" directory
RewriteEngine on
RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]
ServerSignature On
AddDefaultCharset utf-8
</VirtualHost>
Well, the PUT and DELETE still aren't working and returning 403. I'm also worried that I don't really want to allow PUT and DELETE everywhere on the directory, but only through the dummy api directory. What's the right way to do this?
I have managed to solve my question, but I don't really understand why it works:
<VirtualHost *>
ServerAdmin onigame#gmail.com
ServerName servername.com
ServerAlias *.servername.com
DirectoryIndex index.html index.php
DocumentRoot /path/to/local/dir/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory /path/to/local/dir/>
Require all granted
Satisfy All
</Directory>
# RESTful services provided for the fake "api" directory
RewriteEngine on
RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]
ServerSignature On
AddDefaultCharset utf-8
</VirtualHost>
Best I can figure out, getting a 403 means that it's access being blocked, and not the type of HTTP request (which would result in a 405, not a 403). And the access problem is on the local directory, so it needs a special section for it. But I really don't understand why the two lines I put there make things work. The Require directive, that sort of makes sense. But the Satisfy directive, from what I can tell from the documentation, should default to All.
And yet when I remove either line, it doesn't work.
Related
I have just downloaded the latest version of XAMPP with PHP version 7.2.4. I have made a very simple PHP validation for a HTML form and when I press submit it comes up with the following:
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0g PHP/7.2.4
I'd don't know what the problem is as I have tried changing Require none to Require all granted.
Please Help!
I have experienced this problem and found out that localhost link is not configured in
httpd_vhosts.conf.
So I added this line at the bottom part of httpd_vhosts.conf
<VirtualHost *:80>
DocumentRoot "E:/xampp/htdocs"
ServerName localhost
</VirtualHost>
Well, probably this must be happening because the localhost link is not configured in your xamp vhost, try to look for the vhosts configuration file and add the same one there. Just add this block of code making the appropriate path changes until your repository so that you can access the localhost:
# Virtual Hosts
#
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#hcode.com.br
DocumentRoot "C:\ecommerce"
ServerName www.hcodecommerce.com.br
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
<Directory "C:\ecommerce">
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>
</VirtualHost>
By default in httpd.conf your entire system directory "/" is secured and not allowed access
in httpd.conf:
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
Add the below additional under the above
<Directory /home>
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
Change /home to your hosted installation public directory - e.g. /projects or /devsite.local etc...
For more info on Apache see here: https://httpd.apache.org/docs/current/mod/core.html#directory
The 'Options' are typical .htaccess directives - change as needed
The 'AllowOverride All' gives access to the /home folder and everything under it
The 'Require local' makes sure only localhost / 127.0.0.1 has access to folder and files under /home
Hope this helps :D
I have a slight of problem.
I used simple php framework on github and have worked on it and added new functions and so on.
The framework that i have had help with is this:
https://github.com/panique/mini
Everything works perfectly in a MAMP environment, but now when i was going to test it on a live website and apache environment it does not work that good.
It obviously can read the index file since i point the directory to the directory that should be used for apache.
But when i am going to make a call to a controller (domain.com/login) it results in apache giving me :
"Not Found: The requested URL /login was not found on this server."
I have enabled rewrite with: service a2enmod rewrite, and i double checked and it is a Loaded module when looking in phpinfo().
The .htaccess file that is located in the "/var/www/html/php-project/public" directory(where i have pointed to in my virtual host file"
is the following:
# Necessary to prevent problems when using a controller named "index" and having a root index.php
Options -MultiViews
# Activates URL rewriting
RewriteEngine On
# Prevent people from looking directly into folders
Options -Indexes
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
00-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.domain.com
ServerAlias domain.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/php-project/public
<Directory />
AllowOverride All
</Directory>
#DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
apache.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
AccessFileName .htaccess
I dont think the problem is within the php itself.
Thats why i don't added my code since its so many classes, but the code is as i said based on the framework in the beginning of the question.
Do any of you see any obvious problems in the conf's?
I have searched and searched for answers, but i don't seem to get it right. So i would really really appreciate some help.
Any questions, just ask on.
Thanks
Please follow the proper way of access rewrite module in apache2.
First enable the rewrite module in apache using below command-:
$ sudo a2enmod rewrite
Open the apache2.conf file
$ nano /etc/apache2/apache2.conf
Update the section
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Remove the section from 00-default.conf
Remove the below lines
<Directory />
AllowOverride All
</Directory>
Then finally restart your apache2.
$ sudo service apache2 restart
or
$ sudo /etc/init.d/apache2 restart
I keep trying to configure it so that the domain symfony.local would link to the app_dev.php, but I also keep getting forwarded to the xampp page.
My hosts file is:
127.0.0.1 symfony.local
My httpd-vhost is:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName symfony.local
DocumentRoot "C:\xampp\htdocs\testAS\web"
DirectoryIndex app_dev.php
<Directory "C:\xampp\htdocs\testAS\web">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
</IfModule>
</Directory>
</VirtualHost>
I'm using Windows 8. Appreciate any feedback.
I assume it's a fairly recent version of xampp? There is a new directive "Require local" that popped up a few years ago. I remember being stumped by it.
<Directory "C:/home/ahundiak/zayso2016/cerad3/web">
## Options Indexes FollowSymLinks
AllowOverride All
Require local ## Add This
</Directory>
<VirtualHost *:80>
ServerName local.api.zayso.org
DocumentRoot "C:/home/ahundiak/zayso2016/cerad3/web"
</VirtualHost>
I am not an expert on configuring this. The above works for me using the Symfony 2 .htaccess file.
may be it issue of your hosts file entry of symfony.local and local ip address or may between them only TAB(on keyboard) require ...
OR if above answer not work follow below
please follow this link Click Here it help you solve your issue of not show symfony project
Im having some issues with the virtual hosts apache files and htaccess.
I have apache, updated to 2.6 yesterday. I installed 2 virtual domains on it, and disabled the default domain.
All works fine, sites loads fine, no problems there.
The first virtual domain has a htaccess with +50 rewrite conditions and all them work fine!
I tried to add some conditions to the 2nd domain, but the ones that rewrite directories dont work.
This is the condition:
RewriteRule ^user/([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^user/([a-zA-Z0-9_-]+)/$ index.php?username=$1
And there, the "sites-available" apache config files for each domain (only the relevant things):
DOMAIN 1:
ServerAdmin webmaster#localhost
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/domain1.com/public_html
<Directory />
Options FollowSymLinks
AllowOverride ALL
</Directory>
<Directory /var/www/domain1.com/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
allow from all
</Directory>
DOMAIN2:
ServerAdmin webmaster#localhost
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/www/domain2.com/public_html
<Directory />
Options FollowSymLinks
AllowOverride ALL
</Directory>
<Directory /var/www/domain2.com/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
allow from all
</Directory>
As you can see, there is no diference between them other than the path to main folder.
I dont understand whats wrong with it.
Why the first domain works perfect and the second one no.
Some things:
A: Any other condition on the domain2 htaccess file works just fine, like redirec simply html to php, or errorhandlers, or just simply redirects to test.
B: I pasted the same rewrite condition in my first domain htaccess file, and it works perfect!
It looks like the apache config file is not working correctly.
Is there anything I must check/change?
I will appreciate any help, and sorry for my english.
Give this a try its tested and working:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^user/([a-z0-9_-]+)/?$ /index.php?username=$1 [NC,L]
I believe the issue is that you have MultiViews enabled, you can also resume your 2 rules into 1 like I have done above.
This should go into the .htaccess of your domain2.
As a side note, you do not need 2 directories directives, you can remove this one:
<Directory />
Options FollowSymLinks
AllowOverride ALL
</Directory>
And keep just:
<Directory "/var/www/domain2.com/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
allow from all
</Directory>
I'm using the symfony framework to do some PHP development, which uses the same code base for separate sites. This requires me to do some htaccess rewriting which is causing me some pain.
The main index page for developer.mysite.com is developer.php. What I'm trying to is rewrite everything going to developer.mysite.com to www.mysite.com/developer.php/$1. However, in the url, I still want it to say developer.mysite.com/$1
The following code I have now redirects and doesn't keep the original url:
RewriteCond %{HTTP_HOST} ^developer\.mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !\..+$
RewriteRule ^(.*)$ http://www.mysite.com/developer.php/$1 [NC,L]
Are you aliasing developer.mysite.com to mysite.com or does it have it's own public_html (or httpdocs; whatever the webroot is...) folder?
I think the easiest solution would be to point developer.mysite.com to mysite.com on the server so that they share the same public_html folder.
Your main website's front controller would be something like main.php.
The developer subdomain would have its front controller, developer.php.
index.php would look at $_SERVER['SERVER_NAME'] to determine and include the appropriate front controller.
Disclaimer: I'm not familiar with how symfony operates. I'm assuming it has a front controller like a bazillion other frameworks do.
I think that it is better to make separate dir for ur application.
I give an expamle how I realized this task:
<VirtualHost *:80>
DocumentRoot "/path/to/poject/web"
ServerName domain.dev
Alias /sf C:/xampp/php/PEAR/data/symfony/web/sf
<Directory "/path/to/poject/web">
#php_admin_value mbstring.func_overload 7
AllowOverride All
Allow from All
</Directory>
<Directory "C:/xampp/php/PEAR/data/symfony/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/path/to/poject/web_developer"
ServerName developer.domain.dev
Alias /sf C:/xampp/php/PEAR/data/symfony/web/sf
<Directory "/path/to/poject/web_developer">
#php_admin_value mbstring.func_overload 7
AllowOverride All
Allow from All
</Directory>
<Directory "C:/xampp/php/PEAR/data/symfony/web/sf">
AllowOverride All
Allow from All
</Directory>
Alias /css /path/to/poject/web/css
<Directory "/path/to/poject/web/css">
AllowOverride All
Allow from All
</Directory>
Alias /images /path/to/poject/web/images
<Directory "/path/to/poject/web/images">
AllowOverride All
Allow from All
</Directory>
Alias /js /path/to/poject/web/js
<Directory "/path/to/poject/web/js">
AllowOverride All
Allow from All
</Directory>
Alias /sfDoctrinePlugin /path/to/poject/web/sfDoctrinePlugin
<Directory "/path/to/poject/web/sfDoctrinePlugin">
AllowOverride All
Allow from All
</Directory>
Alias /sfFormExtraPlugin /path/to/poject/web/sfFormExtraPlugin
<Directory "/path/to/poject/web/sfFormExtraPlugin">
AllowOverride All
Allow from All
</Directory>
Alias /uploads /path/to/poject/web/uploads
<Directory "/path/to/poject/web/uploads">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
web_developer dir is usual web dir of sf1.4 project