.htaccess for apache 2.2 does not work on apache 2.4 vagrant development core

The .htaccess file below is great for various hosted websites, all on apache 2.2, but the test environment and new websites on cloud servers run on apache 2.4.

The problem is that access to the php file no longer works if the full file name is not added, including .php, for example. www.example.com/about.php , not www.example.com/about

Any help with a lower rating is ideally with .htaccess, which works with both 2.2 and 2.4 apache.

 Options -Indexes +multiviews RewriteEngine On RewriteBase / ErrorDocument 404 /404.php #ErrorDocument 500 /500.php # Add www. RewriteCond %{HTTP_HOST} !^www\. #RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] # Remove .php #RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP #RewriteRule (.*)\.php$ $1 [R=301] # remove index RewriteRule (.*)/index$ $1/ [R=301] # remove slash if not directory RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} /$ RewriteRule (.*)/ $1 [R=301] # add .php to access file, but don't redirect RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] 

--- edit ---

Below is the apache 2.4 000-default.conf which is used for VirtualHost

 php_value auto_prepend_file /vagrant/www/fixdocroot.php <VirtualHost *:80> ServerName dev.dev ServerAlias *.dev UseCanonicalName Off VirtualDocumentRoot "/vagrant/www/%-2+/public_html" <Directory /vagrant/www> AllowOverride All Require all granted </Directory> </VirtualHost> 

--- further edit --- The Apache error log is as follows:

 [Mon Jan 11 09:26:35.751982 2016] [core:notice] [pid 1812] AH00094: Command line: '/usr/sbin/apache2' [Mon Jan 11 09:44:22.816423 2016] [:error] [pid 1892] [client 192.168.33.1:49762] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0, referer: http://speechlink-primary.dev/infant/assessment/start/15 [client 192.168.33.1:49804] AH00687: Negotiation: discovered file(s) matching request: /vagrant/www/domainname/public_html/page-name (None could be negotiated). 
+6
source share
3 answers

The first line of the .htaccess file seems to be a problem:

 Options -Indexes +multiviews 

From the Apache 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 a card of the type that names all of these files, assigning them the same types of media and content encodings that it would have if the client asked one of them by name. He then chooses the best fit for the client.

I believe this interferes with RewriteRules - in my testing on Apache 2.4 boxing, removing +multiviews from this line seems to have solved the problem.

+2
source

I was looking for you a problem because I had the same problem on my server.

I found a solution that works. From Apache docs when upgrading to version 2.4 from 2.2 :

Errors serving requests:

. Htaccess files are not processed. Check the correctness of the AllowOverride directive; The default setting for None is 2.4.

This means that the default value of AllowOverride has been changed from All to None

Default: AllowOverride None (2.3.9 and later), AllowOverride All (2.3.8 and earlier)

So, to fix this problem, you will need to modify your httpd.conf (or apache2.conf ) file by adding AllowOverride All to your Directory block. It will not change any behavior on 2.2 and fix your problem on 2.4.

You can see more information on how to change the Directory block and add AllowOverride All to this nice little stream.

Hope this helps.

+3
source

There are a few things that may be wrong when upgrading from apache 2.2 to 2.4

Forget to turn on the modules

Sometimes you may forget to enable the used modules on a new server, this is easy to solve by running the apache a2enmod <module> command, you use the rewrite module in you .htaccess, and this can be activated using a2enmod rewrite .

The configuration options you used are renamed

In apache 2.4 there are changes in the names of configuration parameters.

According to the apache update notes, the following things have changed:

AllowOverride now None by default.

See @Gal for more on this change.

PhP cannot be installed / enabled on the new server

Although I doubt that this could be the case, I turned it on, as it could be a problem for other people.

Check if php is installed correctly by creating the phpinfo () file, this file contains <?PHP phpinfo() and should provide the correct php information at startup.

Php can be activated on apache using: a2enmod php5 or a2enmod php7

RewriteRule Log Entry

If the rewrite rules continue to fail, a quick test may include rewriting the rule logs , you can enable them by default using apache "LogLevel" to prevent all of our apache from generating debug messages while still receiving debug messages from the rewrite modules, you you need to prefix the latter using its name. For instance:

 LogLevel alert rewrite:trace3 

This will enable rewrite level debugging messages for trace3, you can go to lvl trace8 .

The debug messages of the rewrite module can be read as follows:

[rewrite:trace2][rid#7f9c446360a0/initial] init rewrite engine with requested uri / - This means that the rewrite engine is running for the URL

[rewrite:trace3][rid#7f9c446360a0/initial] applying pattern '^.*$' to uri '/' - This means that a RewriteRule working

[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?projects/old-projects/' => not-matched - This means that a RewriteCond does not match.

[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?' => matched [rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?' => matched - This means that a RewriteCond matches.

[rewrite:trace2][rid#7f9c446360a0/initial] rewrite '/' -> '/index.html' - RewriteRule redirected you to another page externally

The best way to read debug log files is to use a text editor that does not wrap lines, such as less -R on Linux or Notepad on Windows.

+3
source

All Articles