.htaccess for apache 2.2不适用于apache 2.4 vagrant开发盒

bri*_*itt 6 php apache .htaccess

下面的.htaccess文件适用于各种托管网站,所有这些都在apache 2.2上,但云服务器上的测试环境和新网站都在apache 2.4上运行.

问题是访问php文件不再有效,除非添加完整的文件名,包括.php,www.example.com/about.php而不是www.example.com/about

对以下任何帮助都非常赞赏 - 理想情况下使用.htaccess可以同时使用2.2和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]
Run Code Online (Sandbox Code Playgroud)

- -编辑 - -

下面是用于VirtualHost的apache 2.4 000-default.conf

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>
Run Code Online (Sandbox Code Playgroud)

---进一步编辑--- Apache错误日志如下:

[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).
Run Code Online (Sandbox Code Playgroud)

Fer*_*big 5

从apache 2.2升级到2.4时,有一些问题可能会出错

忘记启用模块

有时,您可能忘记在新服务器上启用已使用的模块,这可以通过运行apache命令轻松解决a2enmod <module>,您正在使用rewrite.htaccess中的模块,这可以通过激活a2enmod rewrite.

您重命名的配置参数已重命名

在apache 2.4中,配置参数的名称有一些变化.

根据apache更新说明,以下内容已更改:

AllowOverride现在默认为None.

有关此更改的更多信息,请参阅@ Gal的答案.

可能无法在新服务器上安装/启用PhP

虽然我怀疑情况可能如此,但我把它包括在内,因为这对其他人来说可能是一个问题.

通过制作phpinfo()文件来检查php是否已正确安装,此文件包含<?PHP phpinfo()并且应该在运行时提供正确的php信息.

Php可以在apache上激活:a2enmod php5a2enmod php7

RewriteRule日志记录

如果重写规则仍然失败,快速测试可能会启用重写规则日志,这些可以使用默认的apache"LogLevel"启用,以防止我们的整个apache发出调试消息,同时仍然从重写模块获取调试消息,你需要使用它的名字为后者添加前缀.例如:

LogLevel alert rewrite:trace3
Run Code Online (Sandbox Code Playgroud)

这将打开重写级调试消息到trace3,你可以上升到lvl trace8.

重写模块的调试消息可以如下读取:

[rewrite:trace2][rid#7f9c446360a0/initial] init rewrite engine with requested uri / - 这意味着重写引擎已经启动了一个url

[rewrite:trace3][rid#7f9c446360a0/initial] applying pattern '^.*$' to uri '/'- 这意味着一个RewriteRule有效

[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?projects/old-projects/' => not-matched- 这意味着a RewriteCond没有匹配.

[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?' => matched- 这意味着a RewriteCond匹配了.

[rewrite:trace2][rid#7f9c446360a0/initial] rewrite '/' -> '/index.html' - RewriteRule内部/外部将您重定向到另一个页面

读取调试日志文件的最佳方法是使用不包装行的文本编辑器,例如less -R在Linux上的Linux或记事本上.


kie*_*ajp 3

.htaccess 文件的第一行似乎是问题所在:

Options -Indexes +multiviews
Run Code Online (Sandbox Code Playgroud)

来自Apache 文档

MultiViews的效果如下:如果服务器收到对/some/dir/foo的请求,如果/some/dir启用了MultiViews,并且/some/dir/foo不存在,那么服务器读取目录寻找名为 foo.* 的文件,并有效地伪造一个类型映射,该映射命名所有这些文件,为它们分配相同的媒体类型和内容编码,如果客户端按名称请求其中一个文件,则它们将具有相同的媒体类型和内容编码。然后,它会选择最符合客户要求的方案。

我相信这会干扰 RewriteRules - 在我对 Apache 2.4 vagrant box 的测试中,+multiviews从该行中删除 似乎已经解决了问题。