How to activate rewrite URL from apache2 with Linux - apache

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>

Related

RewriteEngine is not allowed with dynamic <Directory>

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

Akeneo missing files after installation process

After successfully installing the dependencies for Akeneo and the Akeneo pim-community-standard on a Debian 9 machine, I can open the login screen in the frontend on http://myhost.net/app.php
The Login process throws a 404 error on http://myhost.net/form/extensions.
This should be a JSON file (like it is on the demo installation of Akeneo), but it was not built in the installation process. Also when I manually create this file the login process goes on to the next 404:
myhost.next/localization/format/date
myhost.next/rest/security/
myhost.next/rest/user/
Which part of the installation process is supposed to build these files?
http://myhost.net/form/extensions is a call to the Symfony backend so it's not a file to generate during installation.
The 404 error seems linked to the fact that you didn't activate mod_rewrite on your apache configuration.
I add my answer because I lost a lot of time.
Solution for Digital Ocean server
Change vhost file to:
<VirtualHost your_server_ip>
RewriteEngine On
DocumentRoot "/var/www/html/web/"
<Directory /var/www/html/web>
AllowOverride None
Require all granted
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</Directory>
<Directory /var/www/html/>
Options FollowSymlinks
</Directory>
<Directory /var/www/html/web/bundles>
RewriteEngine Off
</Directory>
SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
ErrorLog ${APACHE_LOG_DIR}/akeneo-pim_error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/akeneo-pim_access.log combined
</VirtualHost>
edit your_server_ip
and load entering ip in a browser (without /web/)

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

URL rewriting for cakephp on local OSX apache

I have set up apache on OSX for local web dev use and made a dynamic vhost using dnsmasq such that visiting foo.dev will point to the /foo directory in my ~/Sites/sites folder.
This is what the vhost rule looks like:
<Virtualhost *:80>
VirtualDocumentRoot "/Users/harryg/Sites/sites/%1"
<Directory "/Users/harryg/Sites/sites">
Order allow,deny
Allow from all
Options Indexes FollowSymLinks Includes ExecCGI
# New directive needed in Apache 2.4.3:
Require all granted
AllowOverride All
Satisfy Any
</Directory>
ErrorLog "/Users/harryg/Sites/logs/sites/error.log"
CustomLog "/Users/harryg/Sites/logs/sites/access.log" common
ServerName sites.dev
ServerAlias *.dev
UseCanonicalName Off
</Virtualhost>
This all works great but I donwloaded a copy of cakephp and put it in a directory ~/Sites/sites/cake. When I visit http://cake.dev I get taken to the cakephp default app page which is expected but there is a warning about url rewriting not being properly configured and stylesheets etc do not load. I followed the guide and tried placing the following in the .htaccess for the cake directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
But I then get a 500 Internal Server Error. Error log says:
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.
Suggesting the rewriting rules are conflicting somehow. Any idea how to solve this?
Solved. The answer was to simply add the following line to my .htaccess files:
RewriteBase /
Not sure why it's necessary but it worked!

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.