RewriteEngine is not allowed with dynamic <Directory> - apache

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).

Related

How to activate rewrite URL from apache2 with Linux

I recently transfered all my websites from Windows to KUbuntu via virtual machine. And now I can't access a part of my website that is using rewrite URL...
I've already activated rewrite module with sudo a2enmod rewrite and AllowOverride in apache2 conf and restarted apache but that still does not work...
At the start, I got 404 errors (without default.conf AllowOverride things)
And now I've got a 500 internal error. How don't know how to proceed next, I Googled that problem, but nothing helped me.
EDIT: The 500 internal error happened from my .htaccess:
Header add Access-Control-Allow-Origin "*"
My default.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
RewriteEngine on
</Directory>
</VirtualHost>
My .htaccess
Options -Indexes
RewriteEngine on
# RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# %{HTTP_HOST} = domain
# %{REQUEST_URI} = /vl_web/...
# rewrite pages
RewriteRule ^login$ ./login.php [L]
RewriteRule ^reglement$ ./rules.php [L]
RewriteRule ^changelog$ ./changelog.php [L]
RewriteRule ^government/lspd/$ ./government/panel/?team=LSPD [L]
RewriteRule ^government/bcso/$ ./government/panel/?team=BCSO [L]
# 404 image
# RewriteRule \.(gif|jpe?g|png|bmp) ./assets/img/misc/404.png [NC,L]
# Ht Errors
ErrorDocument 404 /vl_web/assets/resources/hterr/index.php?error=404
ErrorDocument 403 /vl_web/assets/resources/hterr/index.php?error=403
ErrorDocument 500 /vl_web/assets/resources/hterr/index.php?error=500
ErrorDocument 503 /vl_web/assets/resources/hterr/index.php?error=503
Any idea ?
error in my .htaccess /var/www/html/vl_web/.htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
Header is part of mod_headers, which needs to be _enabled. mod_headers is considered an "Extension", in other words:
A module with "Extension" status is not normally compiled and loaded into the server. To enable the module and its functionality, you may need to change the server build configuration files and re-compile Apache. (Source: https://httpd.apache.org/docs/current/mod/module-dict.html#Status)
Although, this will often just need "enabling" in the server config. (Requiring a webserver restart.)
php errors (that do not happen on windows so that's pretty weird): Undefined index: nom in /var/www/html/vl_web/government/panel/files/index/row2.php on line 35, referer: http://192.168.1.29/vl_web/government/
Differences in PHP versions and/or different (default) error_reporting levels could account for the differences in behaviour here. Notably, "Undefined index" messages became an E_WARNING in PHP 7 - previously this was an E_NOTICE.
'LimitInternalRecursion' errors: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://192.168.1.29/vl_web/
This is most probably caused by rewrites in your .htaccess file.
Please add the contents of your .htaccess file to you question.
Of note here is that Windows is a case-insenstive filesystem and Linux is not. The same directive might not match on Linux.
Aside:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
RewriteEngine on
</Directory>
You probably want to disable MultiViews (you are explicitly enabling it above). If you are doing much with mod_rewrite (in .htaccess) then MultiViews can often result in conflicts (depends what you are doing).
Do you really want to enable directory Indexes?
Order and Allow are Apache 2.2 directives. You are evidentally on Apache 2.4 so should be using the Require directive instead.
You do not need to enable the RewriteEngine here unless you are using it in this scope (you are not). If you are using .htaccess then this will most probably override this anyway.
In other words, this should probably be written:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Apache server .htaccess file won't load at all

I know, there are thousands of .htaccess topics out there but I'm looking for days now and can't find an answer.
I want to use .htaccess to redirect from e.g. https://myserver.com/project/about to https://myserver.com/project/about.php using mod_rewrite.
Project structure
in my /var/www/project folder there is
an about.html containing nothing more than a simple <h1> Header
a .htaccess file containing
RewriteEngine on
RewriteRule ^about$ about.html [NC]
I can access the file with https://myserver.com/project/about.php.
What I've tried so far
I've looked at my /etc/apache2 folder (I'm running an ubuntu server, apache v. 2.2.22).
In the internet I've found I should add AllowOverwrite All somewhere but I couldn't find out where. I've found alot of places where it should go in the internet and tried most of them - without success.
Somehow my files all differ from others.
I've also tried to write in the first line of my /var/www/project/.htaccess deny from all or just random things to see if an error 500 gets printed. Nope. No feedback from my .htaccess at all.
Files
/etc/apache2/..
apache2.conf: Containing no <Directory> tag.
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>
httpd.conf: Empty.
conf-available/php5.6-fpm.conf: Containing no <Directory> tag.
sites-available/default: Empty.
sites-enabled/default.conf:
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
sites-enabled/ssl.conf:
<VirtualHost *:443>
...
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
...
</VirtualHost>
I hope you can help me because I literally have no single idea anymore.

.htaccess not working on my Ubuntu 14.04 Distribution

I have just configured my LAMP stack on my Ubuntu 14.04 distribution and want to set .htaccess up to serve a website.
I followed the tutorial https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file and configured a virtual host for my domain, however I am still unable to use the .htaccess file in my projects root, whenever I try to serve a page I get a 404 error.
The .conf file for my domain looks like:
<VirtualHost *:80>
ServerAdmin alexmk92#live.co.uk
ServerName alexsims.me
ServerAlias www.alexsims.me
DocumentRoot /var/www/alexsims.me
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/alexsims.me>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I tried to change AllowOverride None to AllowOverride All but that caused an internal 500 error even after enabling mod_rewrite.
Regards,
Alex.
EDIT: .htaccess contents
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^alexsims.me [NC]
RewriteRule ^(.*)$ http://www.alexsims.me/$1 [L,R=301,NC]
#--- Rewrite PHP files clean URL
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)$ ?page=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ ?page=$1&id=$2 [NC,L]
I have same issue with Ubuntu 15.10.
I solved this way.
First you need to enable rewrite module:
sudo a2enmod rewrite
sudo gedit /etc/apache2/apache2.conf
and replace
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
With:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And finally
sudo service apache2 reload
Actually what is difference between restart and reload !
restart= stop + start
reload = remain running + re-read configuration files.
We have changed configuration so we need to reload configuration.
It should help someone as I have wasted 4 hours :)
Try
Require all granted
in place of
Order allow,deny
allow from all
See the upgrade documentation for more info:
In 2.2, access control based on client hostname, IP address, and other
characteristics of client requests was done using the directives
Order, Allow, Deny, and Satisfy.
In 2.4, such access control is done in the same way as other
authorization checks, using the new module mod_authz_host. The old
access control idioms should be replaced by the new authentication
mechanisms, although for compatibility with old configurations, the
new module mod_access_compat is provided.
AllowOverride None
That's your problem, right there. The 500 error you're getting could mean that your .htaccess file is malformed - start
See http://httpd.apache.org/docs/current/mod/core.html#allowoverride
You should check if the directives you use in .htaccess are enabled.
For example if you use RewriteEngine you should have apache module rewrite enabled:
cat /etc/apache2/mods-available/rewrite.load
a2enmod rewrite
service apache2 restart
For ExpiresActive directive you should enable apache module expires:
cat /etc/apache2/mods-available/expires.load
a2enmod expires
service apache2 restart
etc.

This script is only accessible from localhost

I have a working project with Symfony2.
One of my bundles works well by default but when I activate mod_rewrite I get
This script is only accessible from localhost
This happens only with the routes configured on this bundle, others work fine with mod_rewrite
Here is my vhost config
<VirtualHost *:80>
ServerName my_application.my_domain.net
ServerAdmin xxx#xxx.xxx
DocumentRoot "/var/www/my_application/web"
<Directory /var/www/my_application/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.php [QSA,L]
</IfModule>
</Directory>
</VirtualHost>
Where can that come from ?
Update
Example
I'm getting the error when trying to access
my_application.my_domain.net/config/list/produit
If I disable mod_rewrite I can access
my_application.my_domain.net/app.php/config/list/produit
Are you hitting app_dev.php or config.php?
By default, both of those restrict connections from anywhere but localhost and display that exact same message.
Update
After the update, I think the problem may be because you have MultiViews enabled. MultiViews can try to load config.php even if it is just referenced as config. Try removing that and see if it helps improve anything.

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');